震惊!一小伙用python这么容易就写了个飞机大战?!【含注释】

简介: 周末闲来无事,打算写写代码,但是又不知道写啥,眼睛余光无意中扫到书架上尘封已久的pygame书籍,那干脆写个游戏试一试,要写就写经典游戏吧,毕竟难度也不是很大,开搞。

这种射击游戏相信大多数人玩的很多了,其中要素也是很容易拿捏,所以我先是去网上找了所需要的图片,控制的人物,入侵者,背景图,bgm,用ps修改修改参数,然后就是学习pygame了,简单看了看书,敲了几个例子,不就是用它的库嘛,很简单。因为代码有注释很详细,就不赘述了。


游戏人物所需的基本要素定义

import pygame
import random
import math
from pygame import mixer
# init初始化
pygame.init()
mixer.init()
# 建立画布
screen = pygame.display.set_mode((800, 620))
# 背景
background = pygame.image.load('backgroud.png')
# 设置背景音乐,最好添加绝对路径
show_sound = mixer.Sound('backgroud.wav')
show_sound.play()
# 游戏标题
pygame.display.set_caption("Space Fighting")
# 加载飞船图片
icon = pygame.image.load('transport.png')
# 修改显示窗口的图标
pygame.display.set_icon(icon)
# 玩家操控的人物创建
playerimg = pygame.image.load('transport.png')
# 玩家初始位置
playerx = 370
playery = 540
# x轴改变的距离
playerx_change = 0
# 入侵者的创建
enemyimg = []
enemyx = []
enemyy = []
# 保存入侵者每次位置改变结果
enemyx_change = []
enemyy_change = []
# 入侵者数量
number_of_enemies = 6
# 随机生成入侵者在图上的位置
for i in range(number_of_enemies):
    # 加载入侵者图片
    enemyimg.append(pygame.image.load('enemy.png'))
    # x轴上的坐标
    enemyx.append(random.randint(0, 800))
    # y轴上的坐标
    enemyy.append(random.randint(50, 150))
    # 每次移动改变的距离
    enemyx_change.append(0.5)
    enemyy_change.append(10)
# 子弹创建,加载图片
bulletimg = pygame.image.load('bullet.png')
# 初始距离
bulletx = 0
bullety = 420
# 射出的速度,y轴移动的距离
bulletx_change = 0
bullety_change = 2
# 子弹的状态
bullet_state = "ready"
# 分数记录文字字体
score_value = 0
font = pygame.font.Font('freesansbold.ttf', 32)
# 分数的位置
textx = 10
texty = 10
# 游戏结束文字字体
over_font = pygame.font.Font('freesansbold.ttf', 64)

所需要的几个函数

# 展示分数
def show_score(x, y):
    score = font.render("score :" + str(score_value), True, (255, 255, 255))
    # blit,分数粘贴在背景中
    screen.blit(score, (x, y))
# 游戏结束时GAME OVER
def game_over_text():
    over_txt = over_font.render("GAME OVER", True, (255, 255, 255))  # 文字和颜色
    # blit,将over_txt图片粘在背景图上
    screen.blit(over_txt, (200, 250))  # 展示位置
# 玩家的坐标位置
def player(x, y):
    screen.blit(playerimg, (x, y))
# 入侵者的坐标位置
def enemy(x, y, i):
    screen.blit(enemyimg[i], (x, y))
# 子弹轨迹
def fire_bullet(x, y):
    global bullet_state
    bullet_state = "fire"
    screen.blit(bulletimg, (x + 40, y + 10))
# 子弹轨迹与入侵者的判断
def iscollision(enemyx, enemyy, bulletx, bullety):
    # 判断计算方法
    distance = math.sqrt((math.pow(enemyx - bulletx, 2)) + (math.pow(enemyy - bullety, 2)))
    # 返回的结果
    if distance < 27:
        return True
    else:
        return False

效果

相关文章
|
12天前
|
Python
Python Google风格注释详解
Google风格注释是Python代码注释的一种标准化格式,它提供了一种规范的注释格式,使得代码更加易读、易于维护。Google风格注释使用三个双引号来包围注释内容,并按照一定规范编写。在注释中使用动词短语来描述函数的行为,并使用被动语态。在注释中使用正确的标点符号和缩进,使得注释易于阅读和理解。通过使用Google风格注释,我们可以为代码提供清晰的文档和说明,使得代码更加易读、易于维护。
17 3
|
12天前
|
Python
Python reStructuredText风格注释详解
reStructuredText风格注释是Python代码注释的一种标准化格式,它提供了一种规范的注释格式,使得代码更加易读、易于维护。reStructuredText风格注释使用两个等号来包围注释标题,并按照一定规范编写。通过使用reStructuredText风格注释,我们可以为代码提供清晰的文档和说明,使得代码更加易读、易于维护。
14 2
|
25天前
|
算法 Java Go
【经典算法】LeetCode 67. 二进制求和(Java/C/Python3/Golang实现含注释说明,Easy)
【经典算法】LeetCode 67. 二进制求和(Java/C/Python3/Golang实现含注释说明,Easy)
11 2
|
25天前
|
存储 算法 Java
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
11 2
|
3天前
|
程序员 API 计算机视觉
技术经验解读:【python自动化】02.pywin32库自动操作键鼠(保姆级代码注释)
技术经验解读:【python自动化】02.pywin32库自动操作键鼠(保姆级代码注释)
|
25天前
|
算法 Java Go
【经典算法】LeetCode 69. x 的平方根(Java/C/Python3/Golang实现含注释说明,Easy)
【经典算法】LeetCode 69. x 的平方根(Java/C/Python3/Golang实现含注释说明,Easy)
10 1
|
24天前
|
算法 Python
Python3注释:让你的代码更清晰更易读
Python3注释:让你的代码更清晰更易读
|
25天前
|
算法 Java Go
【经典算法】LeetCode 392 判断子序列(Java/C/Python3/Go实现含注释说明,Easy)
【经典算法】LeetCode 392 判断子序列(Java/C/Python3/Go实现含注释说明,Easy)
20 0
|
25天前
|
算法 Java Go
【经典算法】LeetCode 1103 分糖果 II(Java/C/Python3实现含注释说明,Easy)
【经典算法】LeetCode 1103 分糖果 II(Java/C/Python3实现含注释说明,Easy)
22 0
|
25天前
|
存储 算法 Java
【经典算法】LeetCode112. 路径总和(Java/C/Python3/Go实现含注释说明,Easy)
【经典算法】LeetCode112. 路径总和(Java/C/Python3/Go实现含注释说明,Easy)
11 0