python外星人大战,飞船不移动
本帖最后由 楚子沦i 于 2020-11-9 17:51 编辑解决了,原来我还是不够仔细game_functions.py文件内第9行if event.type == pygame.K_RIGHT:应该把event.type改为event.key!!
如题,边看书边敲的代码,发现飞船不移动。求大佬帮忙看一下!主文件import pygame
from setting import Settings
from ship import Ship
import game_functions as gf
def run_game():
#初始化游戏并且创建一个屏幕对象
pygame.init() #初始化背景属性
ai_settings = Settings()#使类setting里的内容直接被调用
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion") #窗口名字
#创建飞船
ship = Ship(screen)
#开始游戏的主循环
while True:
#监视鼠标和键盘事件
gf.check_events(ship)
gf.update_screen(ai_settings, screen, ship)
run_game()
game_functions.py
import sys
import pygame
def check_events(ship):
"""响应按键和鼠标事件"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.type == pygame.K_RIGHT:
#向右移动飞船
ship.rect.centerx += 1
elif event.type == pygame.K_LEFT:
ship.rect.centerx -= 1
def update_screen(ai_settings, screen, ship):
"""更新数据"""
screen.fill(ai_settings.bg_color)
ship.blitme()
# 让最近绘制的屏幕可见
pygame.display.flip()
ship.py
import pygame
class Ship():
def __init__(self, screen):
"""初始化飞船位置"""
self.screen = screen
#加载飞船图像并获取其外接矩形
self.image =pygame.image.load("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
def blitme(self):
"""在指定位置绘制飞船"""
self.screen.blit(self.image, self.rect)
setting.py
class Settings():
"""储存外星人大战的所有设置"""
def __init__(self):
"""初始化游戏的设置"""
# 屏幕设置
self.screen_width = 1200;
self.screen_height = 800;
self.bg_color = (230, 230, 230)
求大佬帮忙看一些,非常感谢 图灵的那本吧。。。还是我大学时候练习的 ssynihao 发表于 2020-11-9 17:45
图灵的那本吧。。。还是我大学时候练习的
python从入门到实践{:301_1009:} https://blog.csdn.net/friggaaz/article/details/91128318
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
页:
[1]