python pygame模块编写飞机大战
作者:Great__emperor 发布时间:2021-06-30 23:53:48
标签:python,pygame,飞机大战
本文实例为大家分享了python pygame模块编写飞机大战的具体代码,供大家参考,具体内容如下
该程序没有使用精灵组,而是用列表存储对象来替代精灵组的动画效果。用矩形对象的重叠来判断相撞事件。该程序可以流畅运行,注释较为详细,希望可以帮助大家。
import pygame
from pygame.locals import *
from sys import exit
import time
import random
# 创建 * 类,把 * 的图片转化为图像对象,设定固定的移动速度
class Bullet():
def __init__(self,bulletfilename,bulletpos):
self.bulletimg = pygame.image.load(bulletfilename)
self.bullet_rect = self.bulletimg.get_rect()
self.bullet_image = self.bulletimg.subsurface(self.bullet_rect)
self.bullet_rect.midbottom = bulletpos
self.speed = 2
def move(self):
self.bullet_rect.top -= self.speed
# 创建玩家飞机类,用面向对象的思想来对待
class play_plane_fly():
def __init__(self,play_image_filename,play_pos):
self.image = pygame.image.load(play_image_filename)
self.plane_rect = self.image.get_rect()
self.play_image = self.image.subsurface(self.plane_rect)
self.plane_rect.midtop = play_pos
self.speed = 2
# * 是由玩家飞机发射的,所以创建列表,存储 * 对象,使该列表变为该类的属性
self.bullets = []
self.is_hitted = False
# 生成函数,完成发射 * 动作,同时将每个 * 对象存在列表中
def shoot(self,bullet_filename):
bulletobj = Bullet(bullet_filename,self.plane_rect.midtop)
self.bullets.append(bulletobj)
# 向上移动,当飞机移动到边框位置时,无法移动
def moveup(self):
if self.plane_rect.top <= 0:
self.plane_rect.top = 0
else:
self.plane_rect.top -= self.speed
# 向下移动,当飞机移动到边框位置时,无法移动
def movedown(self):
if self.plane_rect.top >= 950 - self.plane_rect.height:
self.plane_rect.top = 950 - self.plane_rect.height
else:
self.plane_rect.top += self.speed
# 向右移动,当飞机移动到边框位置时,无法移动
def moveleft(self):
if self.plane_rect.left <= -40:
self.plane_rect.left = -40
else:
self.plane_rect.left -= self.speed
# 向左移动,当飞机移动到边框位置时,无法移动
def moveright(self):
if self.plane_rect.left >= 700 - self.plane_rect.width:
self.plane_rect.left = 700 - self.plane_rect.width
else:
self.plane_rect.left += self.speed
# 生成敌机类,设定固定的移动速度
class Enemy():
def __init__(self,enemyfilename,enemypos):
self.img = pygame.image.load(enemyfilename)
self.enemy_rect = self.img.get_rect()
self.enemy_image = self.img.subsurface(self.enemy_rect)
self.enemy_rect.midbottom = enemypos
self.speed = 1
def move(self):
self.enemy_rect.bottom += self.speed
clock = pygame.time.Clock()
def main():
# 初始化文字屏幕
pygame.font.init()
# 初始化图像屏幕
pygame.init()
# 设定游戏帧
clock.tick(50)
# 设定游戏屏幕大小
screen = pygame.display.set_mode((660,950))
# 设定游戏名称
pygame.display.set_caption('飞机大战')
# 加载背景图片,生成图像对象
background = pygame.image.load('image/background.png').convert()
backgroundsurface = pygame.transform.scale(background, (660, 950))
# 加载游戏结束图片,生成图像对象
gameover = pygame.image.load('image/gameover.png').convert()
gameoversurface = pygame.transform.scale(gameover,(660, 950))
playplanefilename = 'image/myself.png'
planepos = [330,600]
player = play_plane_fly(playplanefilename,planepos)
bulletfilename = 'image/bullet.png'
# 按频率生成 * ,初始化数字为0
bullet_frequency = 0
enemyfilename = 'image/airplane.png'
# 按频率生成敌机,初始化数字为0
enemy_frequency = 0
enemys = []
myfont = pygame.font.SysFont("arial", 40)
textImage = myfont.render("Score ", True, (0,0,0))
# 初始化得分为0
Score = 0
# 敌机被 * 击中时的动画,将每张图片的图像对象存在列表中
enenys_down = []
enemy0_down = pygame.image.load('image/airplane_ember0.png')
enemy0_down_rect = enemy0_down.get_rect()
enemydown0 = enemy0_down.subsurface(enemy0_down_rect)
enenys_down.append(enemydown0)
enemy1_down = pygame.image.load('image/airplane_ember1.png')
enemy1_down_rect = enemy1_down.get_rect()
enemydown1 = enemy1_down.subsurface(enemy1_down_rect)
enenys_down.append(enemydown1)
enemy2_down = pygame.image.load('image/airplane_ember2.png')
enemy2_down_rect = enemy2_down.get_rect()
enemydown2 = enemy2_down.subsurface(enemy2_down_rect)
enenys_down.append(enemydown2)
enemy3_down = pygame.image.load('image/airplane_ember3.png')
enemy3_down_rect = enemy3_down.get_rect()
enemydown3 = enemy3_down.subsurface(enemy3_down_rect)
enenys_down.append(enemydown3)
while True:
# 动态显示得分
score = str(Score)
myscore = pygame.font.SysFont("arial", 40)
scoreImage = myscore.render(score, True, (0, 0, 0))
# 判断事件,防止卡顿或者意外退出
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
key_pressed = pygame.key.get_pressed()
if key_pressed[K_UP] or key_pressed[K_w]:
player.moveup()
if key_pressed[K_DOWN] or key_pressed[K_s]:
player.movedown()
if key_pressed[K_LEFT] or key_pressed[K_a]:
player.moveleft()
if key_pressed[K_RIGHT] or key_pressed[K_d]:
player.moveright()
screen.blit(backgroundsurface, (0, 0))
if not player.is_hitted:
# 按频率生成 *
if bullet_frequency % 30 == 0:
player.shoot(bulletfilename)
bullet_frequency += 1
if bullet_frequency >= 30:
bullet_frequency = 0
# 让 * 动起来
for i in player.bullets:
i.move()
screen.blit(i.bullet_image,i.bullet_rect)
# 当 * 飞出屏幕,删除 * 对象
if i.bullet_rect.bottom <= 0:
player.bullets.remove(i)
# 按频率生成敌机
if enemy_frequency % 100 == 0:
enemypos = [random.randint(30, 630), 0]
enemyplane = Enemy(enemyfilename, enemypos)
#将敌机对象添加到列表中
enemys.append(enemyplane)
enemy_frequency += 1
if enemy_frequency >= 100:
enemy_frequency = 0
# 让敌机动起来
for i in enemys:
i.move()
screen.blit(i.enemy_image,i.enemy_rect)
# 当敌机飞出屏幕,删除敌机对象
if i.enemy_rect.bottom >= 950:
enemys.remove(i)
# 遍历 * 对象,判断 * 是否击中敌机
for j in player.bullets:
# 如果击中,分数增加,同时移除该 * 和敌机对象
if pygame.Rect.colliderect(j.bullet_rect,i.enemy_rect):
Score += 100
enemys.remove(i)
player.bullets.remove(j)
for k in enenys_down:
screen.blit(k,i.enemy_rect)
# 遍历敌机对象,判断玩家是否和敌机相撞
if pygame.Rect.colliderect(player.plane_rect,i.enemy_rect):
# 修改is_hitted的值,跳出该层循环
player.is_hitted = True
break
screen.blit(player.play_image,player.plane_rect)
screen.blit(textImage, (0,0))
screen.blit(scoreImage, (110, 0))
pygame.display.update()
# 玩家退出时显示分数和游戏结束
else:
screen.blit(gameoversurface,(0,0))
screen.blit(textImage, (0, 0))
screen.blit(scoreImage, (110, 0))
pygame.display.update()
time.sleep(2)
break
main()
来源:https://blog.csdn.net/Great__emperor/article/details/81320133


猜你喜欢
- 这个效果用的很频繁,经常都会有人问我这个问题,所以要把它写成文章。下次再有人问就直接把这篇文章的URL丢出去就好了。这个效果很简单所以我就不
- 目录项目地址安装导入使用1 创建连接2 执行sql语句3 select 方法4 insert_into 方法5 merge_in
- 前言在Python中定义函数,可以用必选参数、默认参数、可变参数和关键字参数,这4种参数都可以一起使用,或者只用其中某些,但是请注意,参数定
- location是javascript里边管理地址栏的内置对象,比如location.href就管理页面的url,用location.hre
- 一、硬件要求首先,TensorFlow-gpu不同于CPU版本的地方在于,GPU版本必须有GPU硬件的支撑。TensorFlow对NVIDI
- 本文实例为大家分享了微信小程序实现顶部搜索框的具体代码,供大家参考,具体内容如下这是一个最简单的顶部搜索框,代码如下wxml<view
- Oracle数据库升级也并非简单的事,这篇文章对Oracle那点事做了较详细的介绍:Oracle数据库升级或数据迁移方法研究我还属于Orac
- jQuery的选择器是CSS 1-3,XPath的结合物。jQuery提取这二种查询语言最好的部分,融合后创造出了最终的jQuery表达式查
- 配合上一篇文章的联系人应用(https://www.aspxhome.com/article/161160.htm),实现配套的基于node
- 如下所示:import tensorflow as tftfe = tf.contrib.eagertf.enable_eager_exec
- 本文实例分析了Python字符串和文件操作常用函数。分享给大家供大家参考。具体如下:# -*- coding: UTF-8 -*-'
- 一、使用多个setting文件 开发Django项目是最常见,也是最麻烦的一个问题就是如何区分开发配置与线上配置。有一些解决方案是
- 什么是事务事务就是一组操作的集合,事务将整组操作作为一个整体,共同提交或者共同撤销这些操作只能同时成功或者同时失败,成功即可提交事务,失败就
- 为何选Nuxt.js?在前后端分离出现之前,传统的web页面都是服务端渲染的,如JSP、PHP、Python Django,还有各种模板技术
- 看了大峡搞的级联菜单,我也班门弄斧一把,嘿嘿,花了一点时间搞了个级联菜单贴上来看看。本例中只要你选择成员分类名称就会自动显示成员名称:&nb
- 数据库事务隔离级别数据库事务的隔离级别有4个,由低到高依次为Read uncommitted:允许脏读。Read committed: 防止
- 1.sorted函数按key值对字典排序先来基本介绍一下sorted函数,sorted(iterable,key,reverse),sort
- 1、Git版本库介绍每个Git版本控制系统的主机中,都可以包含若干个本地版本库,一般情况下一个本地版本库对应一个项目,用于对某个特定项目中的
- 规律:半角空格的 charCode 为 32, 全角空格为 12288. 其他半角字符 ( 33 – 126 ) 与全角 ( 65281 –
- 呵呵,先说明一下下面的程序大部分收集自网络,因为本人在asp编程中经常使用到随机函数,所以收集了一些这类的函数,并做了些注释,方便使用。首发