此部分是gamefunction.py file
import sys
import pygame
def check_keydown_events(event,ship):
"""Respond to the keypressess."""
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.key == pygame.K_LEFT:
ship.moving_left = True
elif 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):
"""Respond to the keyreleases."""
if event.key == pygame.K_RIGHT:
ship.moving_right = False
elif event.key == pygame.K_LEFT:
ship.moving_left = False
elif event.key == pygame.K_UP:
ship.moving_up = False
elif event.key == pygame.K_DOWN:
ship.moving_down = False
def check_events(ship):
"""Respond to keypresses and mouse events."""
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)
这是命名为ship.py
import pygame
class Ship():
def __init__(self,ai_settings,screen):
"""Initialize the ship and set its starting position."""
self.screen = screen
self.ai_settings = ai_settings
#Load the ship image and get its rect.
self.image = pygame.image.load('images/ship.png')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
#Start each new ship at the bottom center of the screen.
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
#Store a decimal value for the ship's center.
self.center = float(self.rect.centerx)
#Movement Flags
self.moving_right = False
self.moving_left = False
self.moving_up = False
self.moving_down = False
def update(self):
"""Update the ship's position based on the movement flag."""
#Update the ship's center value, not the rect
if self.moving_right and self.rect.right<self.screen_rect.right:
self.center += self.ai_settings.ship_speed_factor
if self.moving_left and self.rect.left>0:
self.center -= self.ai_settings.ship_speed_factor
#Up-dwon motion added ***Please check the logic for 1200x800 screen***
if self.moving_up and self.rect.top<self.screen_rect.top:
self.center += self.ai_settings.ship_speed_factor
if self.moving_down and self.rect.bottom>800:
self.center -= self.ai_settings.ship_speed_factor
#Update rect object from self.center.
self.rect.centerx = self.center
来源:Python速效课程
ship.py
中只有几个镊子:
在self.center = float(self.rect.centerx)
下,添加self.bottom = float(self.rect.bottom)
并更改:
if self.moving_up and self.rect.top<self.screen_rect.top:
self.center += self.ai_settings.ship_speed_factor
if self.moving_down and self.rect.bottom>800:
self.center -= self.ai_settings.ship_speed_factor
to:
if self.moving_up and self.rect.top<self.screen_rect.top:
self.bottom -= self.ai_settings.ship_speed_factor
if self.moving_down and self.rect.bottom<800:
self.bottom += self.ai_settings.ship_speed_factor
最后,在self.rect.centerx = self.center
下,添加self.rect.bottom = self.bottom
。
这应该使您能够在游戏中上下上下!
这是我在这些功能中发现的问题:
check_keydown_events
check_keyup_events
check_events
在每种情况下,您都在执行if-elif-elif-elif
语句。这意味着,当if
语句是True
时,elif
语句将无法运行。
在您的情况下,如果按下左键或右键,您将永远无法向上或向下移动。
您应该更改此代码,以便您仅将if-elif
与向上/向下/向左/右移动一起使用。
这是一个(校正(示例:
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.key == pygame.K_LEFT:
ship.moving_left = True
if event.key == pygame.K_UP:
ship.moving_up = True
elif event.key == pygame.K_DOWN:
ship.moving_down = True
如果将每个if-elif-elif-elif
语句更改为if-elif-if-elif
。
ship.py
.....
self.bottom = float(self.rect.bottom)
self.moving_up = False
self.moving_down = False
def update(self):
.....
# check if the ship is moving up and stop the ship to move up if
# it's top edge position is less than
# screen's top (top right corner is 0,0) position
if self.moving_up and self.rect.top > self.screen_rect.top:
self.bottom -= self.ai_settings.ship_speed_factor
# check if the ship is moving down and stop the ship when it's
# bottom position is greater than the screen bottom edge position.
if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
self.bottom += self.ai_settings.ship_speed_factor
self.rect.bottom = self.bottom
game_function.py
def check_keydown_event(event, ship):
"""Respond to keypresses"""
....
elif event.key == pygame.K_UP:
ship.moving_up = True
elif event.key == pygame.K_DOWN:
ship.moving_down = True
def check_keyup_event(event, ship):
....
elif event.key == pygame.K_UP:
ship.moving_up = False
elif event.key == pygame.K_DOWN:
ship.moving_down = False