Pygame开发打飞机游戏

pygame 开发打飞机游戏


一、实验说明

1. 实现效果
单文件实现微信打飞机小游戏
2. 开发环境

python 2.7

pygame

random

sys

3. 源码下载
本课程中的所有源码:http://labfile.oss.aliyuncs.com/courses/49/PythonShootGame.zip


二、项目简介


1. 介绍
本项目来源自Kill-Console博客:http://www.cnblogs.com/dukeleo/p/3339780.html 。游戏将使用 Python 语言开发,主要用到 pygame 的 API。游戏最终将会以 python 源文件 game.py 形式完成,只需要运行game.py就可以进入游戏。
游戏最终效果截图如下:

 


 
2. 知识点
本项目中将介绍在 Windows 桌面环境下使用 Python 及 pygame 快速开发小游戏的方式。可以通过这个游戏入门 pygame 游戏开发。
3. 参考文档
代码参考 Kill-Console 写的PythonShootGame
文档参考 pygame 文档
4. 安装依赖包
需要安装 pygame 库来支持本实验所需的代码运行。

在对应环境中 pip install pygame


三、技术设计


1. 游戏角色
本游戏中所需的角色包括玩家飞机、敌机及子弹。用户可以通过键盘移动玩家飞机在屏幕上的位置来打击不同位置的敌机。因此设计以下 Player,Enemy 和 Bullet 三个类对应三种游戏角色。
对于 Player,需要的操作有射击和移动两种,移动又分为上下左右 4 种情况。
对于 Enemy,则比较简单,只需要移动即可,从屏幕上方出现并移动到屏幕下方。
对于 Bullet,与飞机相同,仅需要以一定速度移动即可。
2. 游戏功能
相信玩过微信打飞机的朋友都熟悉,这里将游戏做了简化。飞机的速度固定,子弹的速度固定,基本操作是移动玩家飞机,目标飞机随机从屏幕上方出现并匀速落到下方,子弹从玩家飞机发出,碰到目标飞机会击毁,如果目标飞机碰到玩家飞机,则 Game Over 并显示分数。


四、代码实现


1. 界面显示
代码实现所需的 resources 图片文件都可以通过下述链接下载:
https://github.com/shiyanlou/PythonShootGame
详细步骤
初始化 pygame
设置游戏界面大小、背景图片及标题
游戏主循环内需要处理游戏界面的初始化、更新及退出
显示玩家飞机(代码中使用的 resources/image/shoot.png 图里包含多种飞机,只需要使用 pygame.image 的 subsurface API 根据位置截取 shoot.png 中所需的图片)
示例代码

#1. 初始化 pygame
pygame.init()


#2. 设置游戏界面大小、背景图片及标题
# 游戏界面像素大小
screen = pygame.display.set_mode((480, 800))


# 游戏界面标题
pygame.display.set_caption('飞机大战')


# 背景图
background = pygame.image.load('resources/image/background.png').convert()


# Game Over 的背景图
game_over = pygame.image.load('resources/image/gameover.png')


# 飞机图片
plane_img = pygame.image.load('resources/image/shoot.png')


# 截取玩家飞机图片
player = plane_img.subsurface(pygame.Rect(0, 99, 102, 126))


#3. 游戏主循环内需要处理游戏界面的初始化、更新及退出
while True:
    # 初始化游戏屏幕
    screen.fill(0)
    screen.blit(background, (0, 0))


    # 显示玩家飞机在位置[200,600]
    screen.blit(player, [200, 600])


    # 更新游戏屏幕
    pygame.display.update()


    # 游戏退出事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()


2. 事件处理

在主循环中处理键盘输入的事件(上下左右按键操作),增加游戏操作交互(玩家飞机的上下左右移动)。
详细步骤
获取键盘事件(上下左右按键)
处理键盘事件(移动飞机的位置)
将上述步骤代码放入游戏主循环中
示例代码

#1. 获取键盘事件(上下左右按键)
key_pressed = pygame.key.get_pressed()


#2. 处理键盘事件(移动飞机的位置)
if key_pressed[K_w] or key_pressed[K_UP]:
    player.moveUp()
if key_pressed[K_s] or key_pressed[K_DOWN]:
    player.moveDown()
if key_pressed[K_a] or key_pressed[K_LEFT]:
    player.moveLeft()
if key_pressed[K_d] or key_pressed[K_RIGHT]:
    player.moveRight()

3. 子弹处理
子弹由玩家飞机发出,并以一定速度向界面上方移动。
详细步骤
生成子弹,需要控制发射频率
以固定速度移动子弹
移动出屏幕后删除子弹
敌机被子弹击中效果处理(下一节处理)
示例代码

#1. 生成子弹,需要控制发射频率
# 首先判断玩家飞机没有被击中
if not player.is_hit:
    if shoot_frequency % 15 == 0:
        player.shoot(bullet_img)
    shoot_frequency += 1
    if shoot_frequency >= 15:
        shoot_frequency = 0


for bullet in player.bullets:
    #2. 以固定速度移动子弹
    bullet.move()
    #3. 移动出屏幕后删除子弹
    if bullet.rect.bottom < 0:
        player.bullets.remove(bullet)            


# 显示子弹
player.bullets.draw(screen)

4. 敌机处理
敌机需要随机在界面上方产生,并以一定速度向下移动。
详细步骤
生成敌机,需要控制生成频率
移动敌机
敌机与玩家飞机碰撞效果处理
移动出屏幕后删除敌机
敌机被子弹击中效果处理
示例代码

#1. 生成敌机,需要控制生成频率
if enemy_frequency % 50 == 0:
    enemy1_pos = [random.randint(0, SCREEN_WIDTH - enemy1_rect.width), 0]
    enemy1 = Enemy(enemy1_img, enemy1_down_imgs, enemy1_pos)
    enemies1.add(enemy1)
enemy_frequency += 1
if enemy_frequency >= 100:
    enemy_frequency = 0




for enemy in enemies1:
    #2. 移动敌机
    enemy.move()
    #3. 敌机与玩家飞机碰撞效果处理
    if pygame.sprite.collide_circle(enemy, player):
        enemies_down.add(enemy)
        enemies1.remove(enemy)
        player.is_hit = True
        break
    #4. 移动出屏幕后删除飞机    
    if enemy.rect.top < 0:
        enemies1.remove(enemy)


#5. 敌机被子弹击中效果处理


# 将被击中的敌机对象添加到击毁敌机 Group 中,用来渲染击毁动画
enemies1_down = pygame.sprite.groupcollide(enemies1, player.bullets, 1, 1)
for enemy_down in enemies1_down:
    enemies_down.add(enemy_down)


# 敌机被子弹击中效果显示
for enemy_down in enemies_down:
    if enemy_down.down_index == 0:
        pass
    if enemy_down.down_index > 7:
        enemies_down.remove(enemy_down)
        score += 1000
        continue
    screen.blit(enemy_down.down_imgs[enemy_down.down_index / 2], enemy_down.rect)
    enemy_down.down_index += 1


# 显示敌机
enemies1.draw(screen)

5. 得分显示
在游戏界面固定位置显示消灭了多少目标敌机。
示例代码

# 绘制得分
score_font = pygame.font.Font(None, 36)
score_text = score_font.render(str(score), True, (128, 128, 128))
text_rect = score_text.get_rect()
text_rect.topleft = [10, 10]
screen.blit(score_text, text_rect)

五、完整代码参考
大家可以按照上述步骤组合出自己的打飞机游戏,也可以参考下列完整代码。
源码实现 Fork 自 Kill-Console 写的PythonShootGame并稍作修改,修改后的代码也开源到 github 上,可以使用下面链接获取:
https://github.com/shiyanlou/PythonShootGame

# -*- coding: utf-8 -*-
import pygame
from sys import exit
from pygame.locals import *
import random


# 设置游戏屏幕大小
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 800


# 子弹类
class Bullet(pygame.sprite.Sprite):
    def __init__(self, bullet_img, init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = bullet_img
        self.rect = self.image.get_rect()
        self.rect.midbottom = init_pos
        self.speed = 10


    def move(self):
        self.rect.top -= self.speed


# 玩家飞机类
class Player(pygame.sprite.Sprite):
    def __init__(self, plane_img, player_rect, init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = []                                 # 用来存储玩家飞机图片的列表
        for i in range(len(player_rect)):
            self.image.append(plane_img.subsurface(player_rect[i]).convert_alpha())
        self.rect = player_rect[0]                      # 初始化图片所在的矩形
        self.rect.topleft = init_pos                    # 初始化矩形的左上角坐标
        self.speed = 8                                  # 初始化玩家飞机速度,这里是一个确定的值
        self.bullets = pygame.sprite.Group()            # 玩家飞机所发射的子弹的集合
        self.img_index = 0                              # 玩家飞机图片索引
        self.is_hit = False                             # 玩家是否被击中


    # 发射子弹
    def shoot(self, bullet_img):
        bullet = Bullet(bullet_img, self.rect.midtop)
        self.bullets.add(bullet)


    # 向上移动,需要判断边界
    def moveUp(self):
        if self.rect.top <= 0:
            self.rect.top = 0
        else:
            self.rect.top -= self.speed


    # 向下移动,需要判断边界
    def moveDown(self):
        if self.rect.top >= SCREEN_HEIGHT - self.rect.height:
            self.rect.top = SCREEN_HEIGHT - self.rect.height
        else:
            self.rect.top += self.speed


    # 向左移动,需要判断边界
    def moveLeft(self):
        if self.rect.left <= 0:
            self.rect.left = 0
        else:
            self.rect.left -= self.speed


    # 向右移动,需要判断边界        
    def moveRight(self):
        if self.rect.left >= SCREEN_WIDTH - self.rect.width:
            self.rect.left = SCREEN_WIDTH - self.rect.width
        else:
            self.rect.left += self.speed


# 敌机类
class Enemy(pygame.sprite.Sprite):
    def __init__(self, enemy_img, enemy_down_imgs, init_pos):
       pygame.sprite.Sprite.__init__(self)
       self.image = enemy_img
       self.rect = self.image.get_rect()
       self.rect.topleft = init_pos
       self.down_imgs = enemy_down_imgs
       self.speed = 2
       self.down_index = 0


    # 敌机移动,边界判断及删除在游戏主循环里处理
    def move(self):
        self.rect.top += self.speed


# 初始化 pygame
pygame.init()


# 设置游戏界面大小、背景图片及标题
# 游戏界面像素大小
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))


# 游戏界面标题
pygame.display.set_caption('飞机大战')


# 背景图
background = pygame.image.load('resources/image/background.png').convert()


# Game Over 的背景图
game_over = pygame.image.load('resources/image/gameover.png')


# 飞机及子弹图片集合
plane_img = pygame.image.load('resources/image/shoot.png')


# 设置玩家飞机不同状态的图片列表,多张图片展示为动画效果
player_rect = []
player_rect.append(pygame.Rect(0, 99, 102, 126))        # 玩家飞机图片
player_rect.append(pygame.Rect(165, 360, 102, 126))
player_rect.append(pygame.Rect(165, 234, 102, 126))     # 玩家爆炸图片
player_rect.append(pygame.Rect(330, 624, 102, 126))
player_rect.append(pygame.Rect(330, 498, 102, 126))
player_rect.append(pygame.Rect(432, 624, 102, 126))
player_pos = [200, 600]
player = Player(plane_img, player_rect, player_pos)


# 子弹图片
bullet_rect = pygame.Rect(1004, 987, 9, 21)
bullet_img = plane_img.subsurface(bullet_rect)


# 敌机不同状态的图片列表,多张图片展示为动画效果
enemy1_rect = pygame.Rect(534, 612, 57, 43)
enemy1_img = plane_img.subsurface(enemy1_rect)
enemy1_down_imgs = []
enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(267, 347, 57, 43)))
enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(873, 697, 57, 43)))
enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(267, 296, 57, 43)))
enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(930, 697, 57, 43)))


enemies1 = pygame.sprite.Group()


# 存储被击毁的飞机,用来渲染击毁动画
enemies_down = pygame.sprite.Group()


# 初始化射击及敌机移动频率
shoot_frequency = 0
enemy_frequency = 0


# 玩家飞机被击中后的效果处理
player_down_index = 16


# 初始化分数
score = 0


# 游戏循环帧率设置
clock = pygame.time.Clock()


# 判断游戏循环退出的参数
running = True


# 游戏主循环
while running:
    # 控制游戏最大帧率为 60
    clock.tick(60)


    # 生成子弹,需要控制发射频率
    # 首先判断玩家飞机没有被击中
    if not player.is_hit:
        if shoot_frequency % 15 == 0:
            player.shoot(bullet_img)
        shoot_frequency += 1
        if shoot_frequency >= 15:
            shoot_frequency = 0


    # 生成敌机,需要控制生成频率
    if enemy_frequency % 50 == 0:
        enemy1_pos = [random.randint(0, SCREEN_WIDTH - enemy1_rect.width), 0]
        enemy1 = Enemy(enemy1_img, enemy1_down_imgs, enemy1_pos)
        enemies1.add(enemy1)
    enemy_frequency += 1
    if enemy_frequency >= 100:
        enemy_frequency = 0


    for bullet in player.bullets:
        # 以固定速度移动子弹
        bullet.move()
        # 移动出屏幕后删除子弹
        if bullet.rect.bottom < 0:
            player.bullets.remove(bullet)   


    for enemy in enemies1:
        #2. 移动敌机
        enemy.move()
        #3. 敌机与玩家飞机碰撞效果处理
        if pygame.sprite.collide_circle(enemy, player):
            enemies_down.add(enemy)
            enemies1.remove(enemy)
            player.is_hit = True
            break
        #4. 移动出屏幕后删除飞机    
        if enemy.rect.top < 0:
            enemies1.remove(enemy)


    #敌机被子弹击中效果处理
    # 将被击中的敌机对象添加到击毁敌机 Group 中,用来渲染击毁动画
    enemies1_down = pygame.sprite.groupcollide(enemies1, player.bullets, 1, 1)
    for enemy_down in enemies1_down:
        enemies_down.add(enemy_down)


    # 绘制背景
    screen.fill(0)
    screen.blit(background, (0, 0))


    # 绘制玩家飞机
    if not player.is_hit:
        screen.blit(player.image[player.img_index], player.rect)
        # 更换图片索引使飞机有动画效果
        player.img_index = shoot_frequency // 8
    else:
        # 玩家飞机被击中后的效果处理
        player.img_index = player_down_index // 8
        screen.blit(player.image[player.img_index], player.rect)
        player_down_index += 1
        if player_down_index > 47:
            # 击中效果处理完成后游戏结束
            running = False


    # 敌机被子弹击中效果显示
    for enemy_down in enemies_down:
        if enemy_down.down_index == 0:
            pass
        if enemy_down.down_index > 7:
            enemies_down.remove(enemy_down)
            score += 1000
            continue
        screen.blit(enemy_down.down_imgs[enemy_down.down_index // 2], enemy_down.rect)
        enemy_down.down_index += 1


    # 显示子弹
    player.bullets.draw(screen)
    # 显示敌机
    enemies1.draw(screen)


    # 绘制得分
    score_font = pygame.font.Font(None, 36)
    score_text = score_font.render(str(score), True, (128, 128, 128))
    text_rect = score_text.get_rect()
    text_rect.topleft = [10, 10]
    screen.blit(score_text, text_rect)


    # 更新屏幕
    pygame.display.update()


    # 处理游戏退出
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()


    # 获取键盘事件(上下左右按键)
    key_pressed = pygame.key.get_pressed()


    # 处理键盘事件(移动飞机的位置)
    if key_pressed[K_w] or key_pressed[K_UP]:
        player.moveUp()
    if key_pressed[K_s] or key_pressed[K_DOWN]:
        player.moveDown()
    if key_pressed[K_a] or key_pressed[K_LEFT]:
        player.moveLeft()
    if key_pressed[K_d] or key_pressed[K_RIGHT]:
        player.moveRight()


# 游戏 Game Over 后显示最终得分
font = pygame.font.Font(None, 48)
text = font.render('Score: '+ str(score), True, (255, 0, 0))
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text_rect.centery = screen.get_rect().centery + 24
screen.blit(game_over, (0, 0))
screen.blit(text, text_rect)


# 显示得分并处理游戏退出
while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    pygame.display.update()

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值