抄一遍书上的代码:
class Settings():
def __init__(self):
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (255, 255, 255)
self.ship_speed_factor = 1.5
import pygame
class Ship():
def __init__(self, ai_settings, screen):
self.screen = screen
self.ai_settings = ai_settings
self.image = pygame.image.load("images/ship.bmp")
self.rect = self.image.get_rect() #图像矩形
self.screen_rect = screen.get_rect() #屏幕矩形,左上角原点,右下角如1200,800
self.rect.centerx = self.screen_rect.centerx #图像x中心,在屏幕中心
self.rect.bottom = self.screen_rect.bottom #图像底边,在屏幕底边
self.center = float(self.rect.centerx) #图像x中心坐标转成浮点型,存在center中
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) #blit实现动画效果
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):
screen.fill(ai_settings.bg_color) #屏幕填充背景色
ship.blitme() #ship的动画效果
pygame.display.flip() #让动画尽可能流畅
alien_invasion.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("Alien Invasion") #设置标题
ship = Ship(ai_settings, screen) #创建游戏对象
while True:
gf.check_events(ship) #检测事件
ship.update() #更新ship位置
gf.update_screen(ai_settings, screen, ship) #刷新屏幕
run_game()
练习题: 1.火箭上下左右移动
2.创建一个程序,显示一个空屏幕。在事件循环中,每当检测到 pygame.KEYDOWN 事件时都打印属性event.key。运行这个程序,并按各种键,看看Pygame如何响应
import pygame
import sys
def check_events(screen):
for event in pygame.event.get():
if event.type == pygame.QUIT: #退出事件
sys.exit()
elif event.type == pygame.KEYDOWN: #键盘按下事件
while True:
font = pygame.font.SysFont("宋体", 80) #设置字体
font_surface = font.render(str(event.key), True, (0, 0, 0), (255, 255, 255)) #渲染内容为面
pygame.image.save(font_surface, "text.bmp") #面保存成图片
image = pygame.image.load("text.bmp") #获取图片
image_rect = image.get_rect() #获取图片的矩形
image_rect.centerx = 200 #设置图片矩形位置
image_rect.centery = 150
screen.blit(image, image_rect) #屏幕呈现该图片
pygame.display.flip() #屏幕刷新显示出来
if pygame.event.poll().type == pygame.KEYUP: #键盘松开
break #退出
def run_game():
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption("测试按键......") #设置标题
while True:
screen.fill((255, 255, 255)) # 屏幕填充背景色
pygame.display.flip() #刷新屏幕
check_events(screen) #检测事件
run_game()
添加子弹模块后的代码 settings.py
class Settings():
def __init__(self):
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (255, 255, 255)
self.ship_speed_factor = 1.5 #飞船速度
self.bullet_speed_factor = 1 #子弹速度
self.bullet_width = 3 #子弹宽高
self.bullet_height = 15
self.bullet_color = 60, 60, 60 #子弹颜色
self.bullets_allowed = 2 #屏幕中允许的子弹数量
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
def __init__(self, ai_settings, screen, ship):
super().__init__()
self.screen = screen
self.rect = pygame.Rect(0, 0, ai_settings.bullet_width,
ai_settings.bullet_height) #绘制子弹的矩形
self.rect.centerx = ship.rect.centerx #子弹x与飞船相同
self.rect.top = ship.rect.top #子弹从飞船顶部起飞
self.y = float(self.rect.y) # y存一份浮点的,可以精调位置
self.color = ai_settings.bullet_color #子弹颜色
self.speed_factor = ai_settings.bullet_speed_factor #子弹速度
def update(self):
self.y -= self.speed_factor #位置更新
self.rect.y = self.y #浮点y位置付给矩形图形
def draw_bullet(self):
pygame.draw.rect(self.screen, self.color, self.rect) #把子弹矩形画到屏幕上
game_functions.py
import sys
import pygame
from bullet import Bullet
def fire_bullet(ai_settings, screen, ship, bullets): #发射子弹
if len(bullets) < ai_settings.bullets_allowed: #子弹没超数量
new_bullet = Bullet(ai_settings, screen, ship) #生成新子弹
bullets.add(new_bullet) #新子弹加入组
def check_keydown_events(event,ai_settings, screen, ship, bullets): #检测键盘按下事件
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.key == pygame.K_LEFT:
ship.moving_left = True
elif event.key == pygame.K_SPACE: #按下空格键,发射子弹
fire_bullet(ai_settings, screen, ship, bullets)
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(ai_settings, screen, ship, bullets):
for event in pygame.event.get():
if event.type == pygame.QUIT: #退出事件
sys.exit()
elif event.type == pygame.KEYDOWN: #键盘按下事件
check_keydown_events(event, ai_settings, screen, ship, bullets)
elif event.type == pygame.KEYUP: #键盘松开事件
check_keyup_events(event, ship)
def update_screen(ai_settings, screen, ship, bullets):
screen.fill(ai_settings.bg_color) #屏幕填充背景色
for bullet in bullets.sprites(): #把每个子弹画在屏幕上
bullet.draw_bullet()
ship.blitme() #ship的动画效果
pygame.display.flip() #刷新,让动画尽可能流畅
def update_bullets(bullets):
bullets.update() #更新子弹位置
for bullet in bullets.copy(): #删除飞出屏幕的子弹
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
alien_invasion.py
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
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("Alien Invasion") #设置标题
ship = Ship(ai_settings, screen) #创建游戏飞船对象
bullets = Group() #创建子弹组
while True:
gf.check_events(ai_settings, screen, ship, bullets) #检测事件
ship.update() #更新ship位置
gf.update_bullets(bullets) #更新子弹位置,删除飞出去的子弹
# print(len(bullets)) #调试时,确认子弹飞出屏幕后被删除
gf.update_screen(ai_settings, screen, ship, bullets) #刷新屏幕
run_game()
作业3:更改上述代码,使之横向发射子弹 添加 pygame.transform.rotozoom(surface, angle, scale) (缩放和旋转) 代码连接 https://github.com/hitskyer/course/tree/master/python/chenmingming/pygame_project/alien_invasion