Python编程从入门到实践第12章,外星人入侵实例中无法获取键盘按键,python第12章,game_functio
Python编程从入门到实践第12章,外星人入侵实例中无法获取键盘按键,python第12章,game_functio
game_functions.py
import pygameimport sysdef check_enents(ship): """响应按键和鼠标事件""" for event in pygame.event.get(): if event == pygame.QUIT: sys.exit() elif event == pygame.KEYDOWN: if event == pygame.K_d: ship.rect.centerx+=10def update_screen(ai_settings,screen,ship): """更新屏幕上的图像""" # 重绘屏幕 screen.fill(ai_settings.bg_color) ship.blitme() pygame.display.flip()
alien_invasion.py
import pygamefrom settings import Settingsfrom ship import Shipimport game_functions as gfdef run_game(): # 初始化游戏,创建屏幕对象 pygame.init() ai_settings=Settings() # 创建设置实例 screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height)) ship=Ship(screen) pygame.display.set_caption("Alien Invasion") # 开始游戏主循环 while True: gf.check_enents(ship) gf.update_screen(ai_settings,screen,ship)run_game()
只能强行终止进程,按红叉无法退出,按d后也没办法向右移动,代码和书上基本一样,求教是哪里错了,和OS X系统有关系么?
找到问题所在了,pygame.QUIT、pygame.KEYDOWN都得用event.tpye,pygame.K_d是表示键盘按下的d,要使用event.key。书上有写但是粗心忽略了……
编橙之家文章,
相关内容
- Python设置代理求助关于爬虫调用requests配置,pythonrequ
- Python gvim自动补全代码配置文件方法,pythongvim,gvim的代
- 迅雷批量下载用Python、django怎么写,pythondjango,python/
- Django模板中指定部分block重写怎么操作,djangoblock,假如
- Python翻转多维序列是如何实现的,python翻转多维序列
- Django服务器报错virtualenv服务器不能运行UnicodeDecodeErro
- python requests 模拟登陆战网失败,pythonrequests,想自己写一
- Python urllib.openurl()不定时会抛出异常是什么情况.,,异常
- 关于python类使用顺序问题的疑惑,python类顺序疑惑,dj
- Python str函数与repr函数两都有何不同,pythonrepr,这两个函
评论关闭