前段时间学了pygame,这两天写一个星球大战小游戏,今天准备分享出来。
开发工具: python3.8、pycharm
代码注释超级详细,在这里就废话不多说了,请看代码!
# 导入相关模块及库
import pygame
import sys
import random
import math
# 1.初始化界面
pygame.init()
# 设置窗口大小
size = width, height = 800, 600
screen = pygame.display.set_mode(size)
# 设置游戏名称
pygame.display.set_caption('星球大战')
# 游戏图标
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
# 3.游戏背景
bgImg = pygame.image.load('bg.png')
# 4.玩家图片
playerImg = pygame.image.load('player.png')
playerX = 400 # X轴设为400
playerY = 530 # Y轴设为530
# 7.设置玩家移动速度
playerStep = 0
def move_player(): # 玩家移动
# 5.设置玩家移动速度
global playerX
playerX += playerStep
# 6.防止玩家出界
if playerX > 736:
playerX = 736
if playerX < 0:
playerX = 0
# 8.敌人
# enemyImg = pygame.image.load('enemy.png')
# enemyX = random.randint(100,600) # X轴随机出现在100~600
# enemyY = random.randint(30,150) # Y轴随机出现在30~150
# enemyStep = 1 # 9.设置敌人移动速度
# 11.添加多个敌人
number_of_enemies = 6 # 敌人总数
class Enemy(): # 定义敌人类
def __init__(self):
self.img = pygame.image.load('enemy.png')
self.x = random.randint(100, 500)
self.y = random.randint(30, 200)
self.step = random.randint(1, 4) # 敌人移动的速度
# 当敌人被击中时,恢复敌人位置
def reset(self):
self.x = random.randint(100, 500)
self.y = random.randint(30, 200)
enemies = [] # 建列表保存敌人
for i in range(number_of_enemies):
enemies.append(Enemy())
# 18.游戏结束
is_over = False
over_font = pygame.font.Font('freesansbold.ttf',32)
def game_over(): # 显示游戏结束
if is_over:
text = 'Game Over!'
render = over_font.render(text, True, (251,1,0)) # 渲染
screen.blit(render,(300,251)) # 分数显示的位置
def show_enemy(): # 显示敌人
# global enemyX, enemyStep, enemyY
global is_over
for e in enemies:
screen.blit(e.img, (e.x, e.y)) # 敌人出现坐标
e.x += e.step # 移动
if (e.x > 736 or e.x < 0):
e.step *= -1
e.y += 150 # 10.下沉150
if e.y > 450:
is_over =True
print('游戏结束!')
enemies.clear() # 游戏结束,清除所有敌人
# 16.添加分数
score = 0
# 12.添加子弹
class Bullet(): # 定义子弹类
def __init__(self):
self.img = pygame.image.load('bullet.png')
self.x = playerX + 16 # 子弹发出去和玩家的正中间的坐标一致 (64-32)/2=16
self.y = playerY + 8 # 子弹发出后在玩家Y轴的基础上+8的位置出现
self.step = 11 # 子弹移动的速度
# 击中
def hit(self):
global score
for e in enemies:
if (distance(self.x, self.y, e.x, e.y) < 30): # 在30之内则击中
hit_sound.play() # 被击中时播放音效
bullets.remove(self) # 子弹消失
e.reset() # 敌人重置
score += 1
print(score)
bullets = [] # 建列表保存现有的子弹
def show_bullets(): # 显示子弹,并移动子弹
for b in bullets:
screen.blit(b.img, (b.x, b.y))
b.hit() # 查看敌人是否被击中
b.y -= b.step # 子弹移动
if b.y < 0: # 当子弹超出顶部时,子弹消失
bullets.remove(b) # 删除子弹
# 欧几里得距离
# 13.两点之间的距离
def distance(bx, by, ex, ey):
x = bx - ex
y = by - ey
return math.sqrt(x * x + y * y) # 开根号
print(distance(1, 1, 4, 5))
# 14.添加背景音乐
pygame.mixer.music.load('bg.wav')
pygame.mixer.music.play(-1) # 单曲循环
# 15.添加击中音效
hit_sound = pygame.mixer.Sound('exp.wav')
# 17.显示分数
font = pygame.font.Font('freesansbold.ttf',32)
def show_score(): # 显示分数
text = f'Score:{score}'
score_render = font.render(text, True, (0,255,125)) # 渲染
screen.blit(score_render,(11,11)) # 分数显示的位置
# 2.游戏主循环
while True:
screen.blit(bgImg, (0, 0)) # 3.画背景,右上角坐标是(0,0),往右是X坐标轴,往下是Y坐标轴
for event in pygame.event.get(): # 事件
if event.type == pygame.QUIT: # 退出
pygame.quit()
sys.exit()
# 7.设置键盘控制飞机移动
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT: # 向右
playerStep = 5
elif event.key == pygame.K_LEFT: # 向左
playerStep = -5
elif event.key == pygame.K_SPACE: # 按空格发射子弹
print('发射子弹...')
# 创建子弹
b = Bullet()
bullets.append(b)
if event.type == pygame.KEYUP: # 向上停止不动
playerStep = 0
screen.blit(playerImg, (playerX, playerY)) # 4.设置玩家坐标
move_player() # 玩家移动
show_enemy() # 显示敌人
show_bullets() # 显示子弹
show_score() # 显示分数
game_over() # 游戏结束
pygame.display.update() # 更新
这是星球大战的简单版本,后期会陆续写新的版本并更新出来!
作者:吴常文
出处:https://blog.csdn.net/qq_41405475
本文版权归作者和CSDN共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。