【发布时间】:2019-09-12 09:16:39
【问题描述】:
更新: 根据答案,我尝试了一些更改。 我改变了ship.py的部分
def update(self):
if self.moving_up and self.rect.top < self.screen_rect.top:
self.rect.centery += self.ai_settings.ship_speed_fator
if self.moving_down and self.rect.bottom > 0:
self.rect.centery -= self.ai_settings.ship_speed_factor
# update the rect based on the self.center
self.rect.centery = self.center
进入下一个:
def update(self):
"""based on the moving signal to change the ship's position"""
#update the ship's center value, not the rect
if self.moving_up and self.rect.top < self.screen_rect.top:
self.rect.center += self.ai_settings.ship_speed_fator
if self.moving_down and self.rect.bottom > 0:
self.rect.center -= self.ai_settings.ship_speed_factor
# update the rect based on the self.center
self.rect.centery = self.center
但仍然无法正常工作。 感觉不好。
我尝试使用教科书教我的相同方式制作类似的pygame,但现在被堆叠了。游戏名为Alien Invasion,被誉为python新手的初试和教科书Python Crash Course的项目。
我已经学习了 Python 速成课程第 12 章,并且我自己输入了教科书的每一行代码。我以为我理解他们。 所以我把游戏转成地平线版本。但我失败了。我无法使用上下键来控制我的船,有人可以帮助我吗?我把它放在我的github上,链接https://github.com/Ruwzy/Python-Crash-Course-Practises/tree/master/PCC_12/Practice_12_5,还有下面的代码。
settings.py
class Settings():
"""Store all the classes of the Alien_Invasion's settings"""
def __init__(self):
""" initialize the game's setting"""
# Screen Settings
self.screen_width = 1600
self.screen_height = 800
self.bg_color = (230, 230, 230)
# ship settings
self.ship_speed_factor = 1.5
game_functions.py
import sys
import pygame
def check_keydown_events(event, ship):
"""response to the keydown"""
if event.key == pygame.K_UP:
ship.moving_up = True
elif event.key == pygame.K_DOWN:
ship.moving_down = True
def check_keyup_events(event, ship):
"""response to the keyup"""
if event.key == pygame.K_UP:
ship.moving_up = False
elif event.key == pygame.K_DOWN:
ship.moving_down = False
def check_events(ship):
"""response to the keyboard and the mouse"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydown_events(event, ship)
elif event.type == pygame.KEYUP:
check_keyup_events(event, ship)
def update_screen(ai_settings, screen, ship):
"""update the screen's image, and change to the new screen"""
# Redraw the screen each time starts the loop
screen.fill(ai_settings.bg_color)
ship.blitme()
# show the lastest drawing screen
pygame.display.flip()
ship.py
import pygame
class Ship():
def __init__(self, ai_settings, screen):
""" initialize the ship and set its original position"""
self.screen = screen
self.ai_settings = ai_settings
# load the ship's image and get its rect
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# set every new ship to the center of left
self.rect.center = self.screen_rect.midleft
self.rect.left = self.screen_rect.left
#store float type value in setting center
self.center = float(self.rect.centery)
# moving signal
self.moving_up = False
self.moving_down = False
def update(self):
"""based on the moving signal to change the ship's position"""
#update the ship's center value, not the rect
if self.moving_up and self.rect.top < self.screen_rect.top:
self.rect.centery += self.ai_settings.ship_speed_fator
if self.moving_down and self.rect.bottom > 0:
self.rect.centery -= self.ai_settings.ship_speed_factor
# update the rect based on the self.center
self.rect.centery = self.center
def blitme(self):
""" draw the ship at a certain position"""
self.screen.blit(self.image, self.rect)
外星人入侵.py
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
""" initialize the game and creat a screen"""
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien_Invasion")
# built a ship
ship = Ship(ai_settings, screen)
# Start the game's main loop
while True:
gf.check_events(ship)
ship.update()
gf.update_screen(ai_settings, screen, ship)
run_game()
我无法让船上下移动,希望有人能帮助我。我确实花了很多时间来调试它。
【问题讨论】:
-
我认为这是因为
ship.update()设置了self.rect.centery = self.center,但self.center在ship.__init__()之后从未改变。 -
我想这可能是问题所在!我会检查它并稍后修复它,然后再试一次。
标签: python python-3.x pygame