类:
import pygame
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 640
class Hero(pygame.sprite.Sprite):
"""玩家"""
def __init__(self, hero_surface, hero_init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = hero_surface
self.rect = self.image.get_rect()
self.rect.topleft = hero_init_pos
self.speed = 6
self.bullets1 = pygame.sprite.Group()
def move(self, offset):
# 左右移动 左减右加
x = self.rect.left + offset[pygame.K_RIGHT] - offset[pygame.K_LEFT]
# 上下移动 下加上减
y = self.rect.top + offset[pygame.K_DOWN] - offset[pygame.K_UP]
# 放置左右移动时出界
if x < 0:
self.rect.left = 0
elif x > SCREEN_WIDTH - self.rect.width:
self.rect.left = SCREEN_WIDTH - self.rect.width
else:
self.rect.left = x
# 防止上下移动时出界
if y < 0:
self.rect.top = 0
elif y > SCREEN_HEIGHT - self.rect.height:
self.rect.top = SCREEN_HEIGHT - self.rect.height
else:
self.rect.top = 0
# 控制射击行为
def single_shoot(self, bullet1_surface):
bullet1 = Bullet(bullet1_surface, self.rect.midtop)
self.bullets1.add(bullet1)
class Bullet(pygame.sprite.Sprite):
"""子弹"""
def __init__(self, bullet_surface, bullet_init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = bullet_surface
self.rect = self.image.get_rect()
self.rect.topleft = bullet_init_pos
self.speed = 8
# 控制子弹移动
def update(self):
self.rect.top -= self.speed
if self.rect.top < -self.rect.height:
self.kill()
Main:
from gameRole import *
# 定义窗口的分辨率
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 640
# 定义画面帧率
FRAME_RATE = 60
# 定义动画周期(帧数)
ANIMATE_CYCLE = 30
# 计数ticks
ticks = 0
offset = {pygame.K_LEFT: 0, pygame.K_RIGHT: 0, pygame.K_UP: 0, pygame.K_DOWN: 0}
# 初始化游戏
pygame.init() # 初始化pygame
clock = pygame.time.Clock()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) # 初始化一个用于显示的窗口
pygame.display.set_caption('飞机大战') # 设置窗口标题
background = pygame.image.load('resources/image/background.png') # 载入背景图
shoot_img = pygame.image.load('resources/image/shoot.png') # 载入资源图片
# 用subsurface剪切读入的图片
hero_surface = []
# hero1_rect = pygame.Rect(0, 99, 102, 126)
hero_surface.append(shoot_img.subsurface(pygame.Rect(0, 99, 102, 126)))
# hero2_rect = pygame.Rect(165, 360, 105, 126)
hero_surface.append((shoot_img.subsurface(pygame.Rect(165, 360, 102, 126))))
# hero1 = shoot_img.subsurface(hero1_rect)
# hero2 = shoot_img.subsurface(hero2_rect)
hero_pos = [200, 600]
# 创建玩家
hero = Hero(hero_surface[0], hero_pos)
bullet1_surface = shoot_img.subsurface(pygame.Rect(1004, 987, 9, 21))
pygame.display.flip()
# 时间循环 (main loop)
while True:
# 控制游戏最大帧率
clock.tick(FRAME_RATE)
screen.blit(background, (0, 0)) # 绘制背景
# 改变飞机图片制造动画
if ticks >= ANIMATE_CYCLE:
ticks = 0
hero.image = hero_surface[ticks//(ANIMATE_CYCLE//2)]
# 绘制飞机
screen.blit(hero.image, hero.rect)
ticks += 1
# 射击
if ticks % 10 == 0:
hero.single_shoot(bullet1_surface)
# 控制子弹
hero.bullets1.update()
# 绘制子弹
hero.bullets1.draw(screen)
pygame.display.update() # 更新屏幕
# 处理游戏退出
# 从消息队列中循环取
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# 控制方向
if event.type == pygame.KEYDOWN:
if event.key in offset:
offset[event.key] = hero.speed
elif event.type == pygame.KEYUP:
if event.key in offset:
offset[event.key] = 0
# 移动飞机
hero.move(offset)