Python的初学者,并从python速成班开发游戏。我基本上是通过控制太空飞船来开发入侵游戏,但是我无法控制方向。
AlienGame.py
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("TemplAReeb's First Game")
ship = Ship(ai_settings, screen)
bg_color = (230, 230, 230)
while True:
gf.check_events(ship)
ship.update()
gf.update_screen(ai_settings, screen, ship)
screen.fill(bg_color)
ship.blitme()
pygame.display.flip()
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
screen.fill(bg_color)
ship.blitme()
pygame.display.flip()
run_game()
game_functions.py
import sys
import pygame
def check_keydown_events(event, ship):
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.key == pygame.K_LEFT:
ship.moving_left = True
def check_keyup_events(event, ship):
if event.key == pygame.K_RIGHT:
ship.moving_right = False
elif event.key == pygame.K_LEFT:
ship.moving_left = False
def check_events(ship):
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):
bg_color = (230, 230, 230)
screen.fill(bg_color)
ship.blitme()
pygame.display.flip()
ship.py
import pygame
class Ship():
def __init__(self, ai_settings, screen):
self.screen = screen
self.ai_settings = ai_settings
self.image = pygame.image.load('D:/Python/Pycharm Projects/GameAlien/images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
self.center = float(self.rect.centerx)
self.moving_right=False
self.moving_left=False
def update(self):
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
self.rect.centerx = self.center
def blitme(self):
self.screen.blit(self.image, self.rect)
当我按下向左箭头键时船应该向左移动,而当我按下向右箭头键时船应该向右移动,但是它要么随机移动要么开始向一个方向移动并且不会停止
答案 0 :(得分:1)
问题是您在主循环中多次调用pygame.event.get()
。请注意,credentials-id
会获取所有消息并将其从队列中删除,因此,第一个或第二个调用会随机获取事件,而另一个调用则一无所获。
在主循环中获取一次事件,并将其传递给函数pygame.event.get()
:
check_events
def check_events(ship, events):
for event in events:
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)
但是请注意,可以使用pygame.key.get_pressed()
来简化代码:
while True:
# the one and only call to `pygame.event.get()`
# get the pending events and clear the event queue
events = pygame.event.get()
for event in events:
if event.type==pygame.QUIT:
sys.exit()
check_events(ship, events)
# [...]
由pygame.event.get()
或pygame.event.poll()
处理事件时,将评估def check_events(ship):
keys = pygame.key.get_pressed()
ship.moving_right = keys[pygame.K_RIGHT]
ship.moving_left = keys[pygame.K_LEFT]
返回的状态