我的第十个项目:开发一个Pygame经典小游戏
你好,我是zhenguo
很久没有发文了,最近几天忙着编写了一个Pygame的经典游戏,坦克飞机大战,作为第10个项目。
前9个项目在这里:
今天与大家一起分享下。
学习编程,透过一些经典游戏,的确更容易引起我们学习的兴趣和动力,在这之前我没怎么使用过Pygame,半个月前开始学习并绘制了一个Pygame基本用法的学习导图:
如果你之前没有任何接触Pygame,不妨先理解一下这个导图,这样再看下面的坦克飞机大战游戏,会更容易理解一些。
坦克飞机大战游戏
首先介绍基本角色,包括:
一架坦克 多架飞机 子弹
基于三个角色,坦克发射子弹,若与飞机碰撞,飞机消失,得分+1;
与此同时,飞机会群会不断靠近坦克,直到越过坦克领域,认为坦克失败,游戏结束。
游戏逻辑
基于角色和游戏规则,需要考虑的游戏逻辑主要包括:
初始逻辑
确定初始位置,绘制坦克 确定飞机数,绘制飞机出现的区域,绘制飞机群 按下空格键,发射子弹,子弹依据坦克位置确定发射位置 确定参数值,包括坦克、飞机、子弹每次移动delta等
初始参数主要包括:
WINDOW_WIDTH, WINDOW_HEIGHT = 800, 600
TANK_INIT_POS = (370, 480)
TANK_MOVE_DELTA = 5
PLANE_MOVE_DELTA = (4, 40)
PLANE_GOAL_POS_Y = 440
PLANE_OCCUR_RANGE_Y = (50, 150)
COLL_DIST_PLANE_BULLET = 27
BULLET_FIRE_RELA_TANK = (16, 10)
初始化逻辑:
import math
import random
import pygame
from pygame import mixer
from config import *
pygame.init()
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("坦克飞机大战")
icon = pygame.image.load('ico.png')
pygame.display.set_icon(icon)
background = pygame.image.load('background.png')
mixer.music.load("bg.wav")
mixer.music.play(-1)
tank_img = pygame.image.load('tank.png')
tank_pos_x, tank_pos_y = TANK_INIT_POS
tank_x_change = 0
plane_imgs = []
plane_x = []
plane_y = []
plane_x_change = []
plane_y_change = []
num_of_planes = 6
for i in range(num_of_planes):
plane_img = pygame.image.load('plane.png')
plane_imgs.append(plane_img)
plane_x.append(random.randint(0, WINDOW_WIDTH - plane_img.get_rect().width))
plane_y.append(random.randint(WINDOW_HEIGHT // 12, WINDOW_HEIGHT // 2))
plane_x_change.append(PLANE_MOVE_DELTA[0])
plane_y_change.append(PLANE_MOVE_DELTA[1])
bullet_img = pygame.image.load('bullet.png')
bullet_x = 0
bullet_y = tank_pos_y
bullet_y_change = 10
bullet_state = "ready"
score_value = 0
font = pygame.font.Font(None, 32)
text_x = 10
text_y = 10
over_font = pygame.font.Font(None, 64)
游戏循环逻辑
按下右(左)箭头,坦克右(左)移动 检测坦克是否到了游戏窗口边界 按下空格发射子弹,子弹每次移动delta 飞机群每次移动 检测子弹是否命中其中一架飞机,若命中,飞机和子弹消失,得分+1 检测飞机是否越过坦克领地,若是游戏结束
转化为对应的代码为:
if __name__ == "__main__":
running = True
while running:
screen.fill((0, 0, 0))
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
tank_x_change = -TANK_MOVE_DELTA
if event.key == pygame.K_RIGHT:
tank_x_change = TANK_MOVE_DELTA
if event.key == pygame.K_SPACE:
if bullet_state == "ready":
mixer.Sound("laser.wav").play()
bullet_x = tank_pos_x
fire_bullet(bullet_x, bullet_y)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
tank_x_change = 0
tank_pos_x += tank_x_change
if tank_pos_x <= 0:
tank_pos_x = 0
elif tank_pos_x >= WINDOW_WIDTH - plane_img.get_rect().width:
tank_pos_x = WINDOW_WIDTH - plane_img.get_rect().width
for i in range(num_of_planes):
if plane_y[i] > PLANE_GOAL_POS_Y:
for j in range(num_of_planes):
plane_y[j] = 2000
game_over_text()
break
plane_x[i] += plane_x_change[i]
if plane_x[i] <= 0:
plane_x_change[i] = PLANE_MOVE_DELTA[0]
plane_y[i] += plane_y_change[i]
elif plane_x[i] >= WINDOW_WIDTH - plane_img.get_rect().width:
plane_x_change[i] = -PLANE_MOVE_DELTA[0]
plane_y[i] += plane_y_change[i]
collision = is_collision(plane_x[i], plane_y[i], bullet_x, bullet_y)
if collision:
explosion_sound = mixer.Sound("explosion.wav")
explosion_sound.play()
bullet_y = tank_pos_y
bullet_state = "ready"
score_value += 1
plane_x[i] = random.randint(0, WINDOW_WIDTH - plane_img.get_rect().width)
plane_y[i] = random.randint(PLANE_OCCUR_RANGE_Y[0], PLANE_OCCUR_RANGE_Y[1])
draw_plane(plane_x[i], plane_y[i], i)
if bullet_y <= 0:
bullet_y = tank_pos_y
bullet_state = "ready"
if bullet_state == "fire":
fire_bullet(bullet_x, bullet_y)
bullet_y -= bullet_y_change
draw_tank(tank_pos_x, tank_pos_y)
show_score(text_x, text_y)
pygame.display.update()
游戏界面展示如下:
完整代码
坦克飞机大战游戏,尤其适合想提升动手编码能力的小伙伴。现在我把完整代码分享出来,大家只需要关注我的视频号,并在我的视频号里回复:飞机
关注并回复:飞机
下载完整代码
我的课程宣传
最近我录制了120节从零学Python精品视频课,讲解细致用心,一共13章节,每章都有思考题和案例,2个月认真学习2遍,Python编程功底提升一大截,再学爬虫、数据分析、web开发和算法工作,都会做的更快、更扎实。
实话讲,我本想定价在2999元,毕竟是实实际际的精品课程,
并且我会不断扩充更多课(比如数据分析、算法等)到这个课中,
所以相当于花一门课的钱,学到了很多门课
你说值不值?!
但是我看了后台数据,
关注我的很多都是在校大学生,所以我直接定在299元,更优惠的是,再放出10个168元的名额。
这个课我会提供班级群,会在里面答疑每一个提问。
所以现在买,绝对是最优惠的时候。后面的价格绝对不会低于168元。
长按图中圆圈二维码
168元抢,仅10个名额
阅读原文 关键词
飞机
子弹
pygame.display
pygame.image.load
random.randint
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。