# 昨日知识点回顾
设置背景颜色
在屏幕中央绘制飞船
# 今日知识点学习
12.5 重构:方法_check_events()和_update_screen()
12.5.1 方法_check_events()
import sys
import pygame
from Settings import Settings
from Ship import Ship
class 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 Ship
class 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 Ship
class 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 += 1
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.2 允许持续移动
#Ship.py
import pygame
class Ship:
"""管理飞船的类"""
def __init__(self, ai_game):
"""初始化飞船并设置其初始位置"""
self.screen = ai_game.screen
self.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 = False
def update(self):
"""根据移动标志调整飞船的位置"""
if self.moving_right:
self.rect.x += 1
def blitme(self):
"""在指定位置绘制飞船"""
self.screen.blit(self.image, self.rect)
# 主程序
import sys
import pygame
from Settings import Settings
from Ship import Ship
class 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 = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
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.3 左右移动
# Ship.py
import pygame
class Ship:
"""管理飞船的类"""
def __init__(self, ai_game):
"""初始化飞船并设置其初始位置"""
self.screen = ai_game.screen
self.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 = False
self.moving_left =False
def update(self):
"""根据移动标志调整飞船的位置"""
if self.moving_right:
self.rect.x += 1
if self.moving_left:
self.rect.x -= 1
def blitme(self):
"""在指定位置绘制飞船"""
self.screen.blit(self.image, self.rect)
# 主程序
import sys
import pygame
from Settings import Settings
from Ship import Ship
class 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 = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
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.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 pygame
class Ship:
"""管理飞船的类"""
def __init__(self, ai_game):
"""初始化飞船并设置其初始位置"""
self.screen = ai_game.screen
self.settings = ai_game.settings
self.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 = False
self.moving_left = False
def update(self):
"""根据移动标志调整飞船的位置"""
# 更新飞船而不是rect对象的x值
if self.moving_right:
self.rect.x += self.settings.ship_speed
if self.moving_left:
self.rect.x -= self.settings.ship_speed
# 根据self.x 更新rect对象
self.rect.x = self.x
def blitme(self):
"""在指定位置绘制飞船"""
self.screen.blit(self.image, self.rect)
# 主程序
import sys
import pygame
from Settings import Settings
from Ship import Ship
class 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().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 = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif event.key == pygame.K_q:
sys.exit()
def _check_keyup_events(self, event):
"""响应松开"""
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
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()
# 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 pygame
class Ship:
"""管理飞船的类"""
def __init__(self, ai_game):
"""初始化飞船并设置其初始位置"""
self.screen = ai_game.screen
self.settings = ai_game.settings
self.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 = False
self.moving_left = False
def 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.x
def 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 Ship
class 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 = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
def _check_keyup_events(self, event):
"""响应松开"""
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
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.7 按Q键退出
def _check_keydown_events(self, event):
"""响应按键"""
if event.key == pygame.K_RIGHT:
self.ship.moving_right = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif 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 Ship
class 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().width
self.settings.screen_height = self.screen.get_rect().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 = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif event.key == pygame.K_q:
sys.exit()
def _check_keyup_events(self, event):
"""响应松开"""
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
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()