# 昨日知识点回顾
设置背景颜色
在屏幕中央绘制飞船
# 今日知识点学习
12.5 重构:方法_check_events()和_update_screen()
12.5.1 方法_check_events()
import sys
import pygame
from Settings import Settings
from Ship import Shipclass AlienInvasion:"""管理游戏资源和行为的类"""def __init__(self):"""初始化游戏并创建游戏资源"""pygame.init()self.settings = Settings()self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))pygame.display.set_caption("Alien Invasion")self.ship = Ship(self)def run_game(self):"""开始游戏的主循环"""while True:self._check_events()# 每次循环是都重绘屏幕self.screen.fill(self.settings.bg_color)self.ship.blitme()def _check_events(self):"""响应按键和鼠标事件"""for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()# screen.fill(bg_color)# screen = pygame.display.set_mode([1200, 800])# 让最近绘制的屏幕可见pygame.display.flip()if __name__ == '__main__':# 创建游戏实例并运行游戏ai = AlienInvasion()ai.run_game()
12.5.2 方法_update_screen()
import sys
import pygame
from Settings import Settings
from Ship import Shipclass AlienInvasion:"""管理游戏资源和行为的类"""def __init__(self):"""初始化游戏并创建游戏资源"""pygame.init()self.settings = Settings()self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))pygame.display.set_caption("Alien Invasion")self.ship = Ship(self)def run_game(self):"""开始游戏的主循环"""while True:self._check_events()self._update_screen()def _check_events(self):"""响应按键和鼠标事件"""for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()# screen.fill(bg_color)# screen = pygame.display.set_mode([1200, 800])def _update_screen(self):"""更新屏幕上的图像,并切换到新屏幕。"""self.screen.fill(self.settings.bg_color)self.ship.blitme()# 让最近绘制的屏幕可见pygame.display.flip()if __name__ == '__main__':# 创建游戏实例并运行游戏ai = AlienInvasion()ai.run_game()
12.6 驾驶飞船
12.6.1 响应按键
import sys
import pygame
from Settings import Settings
from Ship import Shipclass AlienInvasion:"""管理游戏资源和行为的类"""def __init__(self):"""初始化游戏并创建游戏资源"""pygame.init()self.settings = Settings()self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))pygame.display.set_caption("Alien Invasion")self.ship = Ship(self)def run_game(self):"""开始游戏的主循环"""while True:self._check_events()self._update_screen()def _check_events(self):"""响应按键和鼠标事件"""for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type == pygame.KEYDOWN:if event.key == pygame.K_RIGHT:# 向右移动飞船self.ship.rect.x += 1def _update_screen(self):"""更新屏幕上的图像,并切换到新屏幕。"""self.screen.fill(self.settings.bg_color)self.ship.blitme()# 让最近绘制的屏幕可见pygame.display.flip()if __name__ == '__main__':# 创建游戏实例并运行游戏ai = AlienInvasion()ai.run_game()
12.6.2 允许持续移动
#Ship.py
import pygameclass Ship:"""管理飞船的类"""def __init__(self, ai_game):"""初始化飞船并设置其初始位置"""self.screen = ai_game.screenself.screen_rect = ai_game.screen.get_rect()# 加载飞船图像并获取其外接矩形self.image = pygame.image.load('images/ship1.bmp')self.rect = self.image.get_rect()# 将每艘飞船放在屏幕底部中央self.rect.midbottom = self.screen_rect.midbottom# 移动标志self.moving_right = Falsedef update(self):"""根据移动标志调整飞船的位置"""if self.moving_right:self.rect.x += 1def blitme(self):"""在指定位置绘制飞船"""self.screen.blit(self.image, self.rect)# 主程序
import sys
import pygame
from Settings import Settings
from Ship import Shipclass AlienInvasion:"""管理游戏资源和行为的类"""def __init__(self):"""初始化游戏并创建游戏资源"""pygame.init()self.settings = Settings()self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))pygame.display.set_caption("Alien Invasion")self.ship = Ship(self)def run_game(self):"""开始游戏的主循环"""while True:self._check_events()self.ship.update()self._update_screen()def _check_events(self):"""响应按键和鼠标事件"""for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type == pygame.KEYDOWN:if event.key == pygame.K_RIGHT:self.ship.moving_right = Trueelif event.type == pygame.KEYUP:if event.key == pygame.K_RIGHT:self.ship.moving_right = Falsedef _update_screen(self):"""更新屏幕上的图像,并切换到新屏幕。"""self.screen.fill(self.settings.bg_color)self.ship.blitme()# 让最近绘制的屏幕可见pygame.display.flip()if __name__ == '__main__':# 创建游戏实例并运行游戏ai = AlienInvasion()ai.run_game()
12.6.3 左右移动
# Ship.py
import pygameclass Ship:"""管理飞船的类"""def __init__(self, ai_game):"""初始化飞船并设置其初始位置"""self.screen = ai_game.screenself.screen_rect = ai_game.screen.get_rect()# 加载飞船图像并获取其外接矩形self.image = pygame.image.load('images/ship1.bmp')self.rect = self.image.get_rect()# 将每艘飞船放在屏幕底部中央self.rect.midbottom = self.screen_rect.midbottom# 移动标志self.moving_right = Falseself.moving_left =Falsedef update(self):"""根据移动标志调整飞船的位置"""if self.moving_right:self.rect.x += 1if self.moving_left:self.rect.x -= 1def blitme(self):"""在指定位置绘制飞船"""self.screen.blit(self.image, self.rect)# 主程序
import sys
import pygame
from Settings import Settings
from Ship import Shipclass AlienInvasion:"""管理游戏资源和行为的类"""def __init__(self):"""初始化游戏并创建游戏资源"""pygame.init()self.settings = Settings()self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))pygame.display.set_caption("Alien Invasion")self.ship = Ship(self)def run_game(self):"""开始游戏的主循环"""while True:self._check_events()self.ship.update()self._update_screen()def _check_events(self):"""响应按键和鼠标事件"""for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type == pygame.KEYDOWN:if event.key == pygame.K_RIGHT:self.ship.moving_right = Trueelif event.key == pygame.K_LEFT:self.ship.moving_left = Trueelif event.type == pygame.KEYUP:if event.key == pygame.K_RIGHT:self.ship.moving_right = Falsedef _update_screen(self):"""更新屏幕上的图像,并切换到新屏幕。"""self.screen.fill(self.settings.bg_color)self.ship.blitme()# 让最近绘制的屏幕可见pygame.display.flip()if __name__ == '__main__':# 创建游戏实例并运行游戏ai = AlienInvasion()ai.run_game()
12.6.4 调整飞船的速度
# Settings.py
class Settings():"""存储《外星人入侵》的所有设置的类"""def __init__(self):"""初始化游戏的设置"""# 屏幕设置self.screen_width = int(1200)self.screen_height = int(800)self.bg_color = (230, 230, 230)# 飞船设置self.ship_speed = 1.5# Ship.py
import pygameclass Ship:"""管理飞船的类"""def __init__(self, ai_game):"""初始化飞船并设置其初始位置"""self.screen = ai_game.screenself.settings = ai_game.settingsself.screen_rect = ai_game.screen.get_rect()# 加载飞船图像并获取其外接矩形self.image = pygame.image.load('images/ship1.bmp')self.rect = self.image.get_rect()# 将每艘飞船放在屏幕底部中央self.rect.midbottom = self.screen_rect.midbottom# 在飞船的属性x中存储小数值self.x = float(self.rect.x)# 移动标志self.moving_right = Falseself.moving_left = Falsedef update(self):"""根据移动标志调整飞船的位置"""# 更新飞船而不是rect对象的x值if self.moving_right:self.rect.x += self.settings.ship_speedif self.moving_left:self.rect.x -= self.settings.ship_speed# 根据self.x 更新rect对象self.rect.x = self.xdef blitme(self):"""在指定位置绘制飞船"""self.screen.blit(self.image, self.rect)# 主程序
import sys
import pygame
from Settings import Settings
from Ship import Shipclass AlienInvasion:"""管理游戏资源和行为的类"""def __init__(self):"""初始化游戏并创建游戏资源"""pygame.init()self.settings = Settings()# 非全屏运行self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))# 全屏运行# self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)# self.settings.screen_width = self.screen.get_rect().width# self.settings.screen_height = self.screen.get_rect().heightpygame.display.set_caption("Alien Invasion")self.ship = Ship(self)def run_game(self):"""开始游戏的主循环"""while True:self._check_events()self.ship.update()self._update_screen()def _check_events(self):"""响应按键和鼠标事件"""for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type == pygame.KEYDOWN:self._check_keydown_events(event)elif event.type == pygame.KEYUP:self._check_keyup_events(event)def _check_keydown_events(self, event):"""响应按键"""if event.key == pygame.K_RIGHT:self.ship.moving_right = Trueelif event.key == pygame.K_LEFT:self.ship.moving_left = Trueelif event.key == pygame.K_q:sys.exit()def _check_keyup_events(self, event):"""响应松开"""if event.key == pygame.K_RIGHT:self.ship.moving_right = Falseelif event.key == pygame.K_LEFT:self.ship.moving_left = Falsedef _update_screen(self):"""更新屏幕上的图像,并切换到新屏幕。"""self.screen.fill(self.settings.bg_color)self.ship.blitme()# 让最近绘制的屏幕可见pygame.display.flip()if __name__ == '__main__':# 创建游戏实例并运行游戏ai = AlienInvasion()ai.run_game()# debug运行显示:
# pydev debugger: CRITICAL WARNING: This version of python seems to be incorrectly compiled (internal generated filenames are not absolute)
# pydev debugger: The debugger may still function, but it will work slower and may miss breakpoints.
# pydev debugger: Related bug: http://bugs.python.org/issue1666807
# -------------------------------------------------------------------------------
# pydev debugger: Unable to find real location for: <frozen codecs>
# pydev debugger: Unable to find real location for: <frozen importlib._bootstrap>
# pydev debugger: Unable to find real location for: <frozen importlib._bootstrap_external>
# pydev debugger: Unable to find real location for: <frozen ntpath>
# pydev debugger: Unable to find real location for: <frozen os>
# pydev debugger: Unable to find real location for: <frozen zipimport>
# pydev debugger: Unable to find real location for: <frozen _collections_abc>
# pygame 2.5.2 (SDL 2.28.3, Python 3.11.5)
# Hello from the pygame community. https://www.pygame.org/contribute.html
# pydev debugger: Unable to find real location for: <frozen posixpath>
# pydev debugger: Unable to find real location for: <frozen genericpath># 编译配置解释器选项设置-Xfrozen_modules=off 治标不治本
运行结果:(与之前飞船能向左或向右移动到屏幕之外不同,飞船纹丝不动)
这里面有问题
debug显示pydev debugger: Unable to find real location for: <frozen codecs>
参考Unable to find real location for: <frozen codecs> - 知乎 (zhihu.com)
解决无果,有待进一步研究解决 ---未完待续---
12.6.5 限制飞船的活动范围
# Ship.py
import pygameclass Ship:"""管理飞船的类"""def __init__(self, ai_game):"""初始化飞船并设置其初始位置"""self.screen = ai_game.screenself.settings = ai_game.settingsself.screen_rect = ai_game.screen.get_rect()# 加载飞船图像并获取其外接矩形self.image = pygame.image.load('images/ship1.bmp')self.rect = self.image.get_rect()# 将每艘飞船放在屏幕底部中央self.rect.midbottom = self.screen_rect.midbottom# 在飞船的属性x中存储小数值self.x = float(self.rect.x)# 移动标志self.moving_right = Falseself.moving_left = Falsedef update(self):"""根据移动标志调整飞船的位置"""# 更新飞船而不是rect对象的x值# 如果飞船右移的标志和飞船外接矩形右边缘的x坐标 小于 屏幕右边缘x坐标,开始右移if self.moving_right and self.rect.right < self.screen_rect.right:self.rect.x += self.settings.ship_speed# 如果飞船左移的标志和飞船外接矩形左边缘的x坐标 大于于 0,开始左移if self.moving_left and self.rect.left > 0:self.rect.x -= self.settings.ship_speed# 根据self.x 更新rect对象self.rect.x = self.xdef blitme(self):"""在指定位置绘制飞船"""self.screen.blit(self.image, self.rect)
12.6.6 重构_check_events()
import sys
import pygame
from Settings import Settings
from Ship import Shipclass AlienInvasion:"""管理游戏资源和行为的类"""def __init__(self):"""初始化游戏并创建游戏资源"""pygame.init()self.settings = Settings()self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))pygame.display.set_caption("Alien Invasion")self.ship = Ship(self)def run_game(self):"""开始游戏的主循环"""while True:self._check_events()self.ship.update()self._update_screen()def _check_events(self):"""响应按键和鼠标事件"""for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type == pygame.KEYDOWN:self._check_keydown_events(event)elif event.type == pygame.KEYUP:self._check_keyup_events(event)def _check_keydown_events(self, event):"""响应按键"""if event.key == pygame.K_RIGHT:self.ship.moving_right = Trueelif event.key == pygame.K_LEFT:self.ship.moving_left = Truedef _check_keyup_events(self, event):"""响应松开"""if event.key == pygame.K_RIGHT:self.ship.moving_right = Falseelif event.key == pygame.K_LEFT:self.ship.moving_left = Falsedef _update_screen(self):"""更新屏幕上的图像,并切换到新屏幕。"""self.screen.fill(self.settings.bg_color)self.ship.blitme()# 让最近绘制的屏幕可见pygame.display.flip()if __name__ == '__main__':# 创建游戏实例并运行游戏ai = AlienInvasion()ai.run_game()
12.6.7 按Q键退出
def _check_keydown_events(self, event):"""响应按键"""if event.key == pygame.K_RIGHT:self.ship.moving_right = Trueelif event.key == pygame.K_LEFT:self.ship.moving_left = Trueelif event.key == pygame.K_q:sys.exit()
12.6.8 在全屏模式下运行游戏
self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) self.settings.screen_width = self.screen.get_rect().width self.settings.screen_height = self.screen.get_rect().height
import sys
import pygame
from Settings import Settings
from Ship import Shipclass AlienInvasion:"""管理游戏资源和行为的类"""def __init__(self):"""初始化游戏并创建游戏资源"""pygame.init()self.settings = Settings()self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)self.settings.screen_width = self.screen.get_rect().widthself.settings.screen_height = self.screen.get_rect().heightpygame.display.set_caption("Alien Invasion")self.ship = Ship(self)def run_game(self):"""开始游戏的主循环"""while True:self._check_events()self.ship.update()self._update_screen()def _check_events(self):"""响应按键和鼠标事件"""for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type == pygame.KEYDOWN:self._check_keydown_events(event)elif event.type == pygame.KEYUP:self._check_keyup_events(event)def _check_keydown_events(self, event):"""响应按键"""if event.key == pygame.K_RIGHT:self.ship.moving_right = Trueelif event.key == pygame.K_LEFT:self.ship.moving_left = Trueelif event.key == pygame.K_q:sys.exit()def _check_keyup_events(self, event):"""响应松开"""if event.key == pygame.K_RIGHT:self.ship.moving_right = Falseelif event.key == pygame.K_LEFT:self.ship.moving_left = Falsedef _update_screen(self):"""更新屏幕上的图像,并切换到新屏幕。"""self.screen.fill(self.settings.bg_color)self.ship.blitme()# 让最近绘制的屏幕可见pygame.display.flip()if __name__ == '__main__':# 创建游戏实例并运行游戏ai = AlienInvasion()ai.run_game()