python飞机大战代码
import pygame
from pygame.locals import *
from pygame.sprite import Sprite
import random
import time
pygame.init()#游戏初始化
pygame.mixer.init()#混音器初始化
#游戏背景音乐
pygame.mixer.music.load("./sound/game_music.wav")
pygame.mixer.music.set_volume(0.2)
#子弹发射音乐
bullet_sound = pygame.mixer.Sound("./sound/bullet.wav")
bullet_sound.set_volume(0.2)
#我方飞机挂了的音乐
me_down_sound = pygame.mixer.Sound("./sound/game_over.wav")
me_down_sound.set_volume(0.2)
#敌方飞机挂了的音乐
enemy_down_sound = pygame.mixer.Sound("./sound/enemy1_down.wav")
enemy_down_sound.set_volume(0.2) #设置定时器事件
CREAT_ENEMY = pygame.USEREVENT
pygame.time.set_timer(CREAT_ENEMY,1000) #创建一个窗口,用来显示内容
screen = pygame.display.set_mode((480,800),0,32) class Base(object):
def __init__(self,screen):
self.screen = screen class Plan(Base):
def __init__(self,screen):
super().__init__(screen) self.image = pygame.image.load(self.imageName).convert()
self.bulletList = []
def display(self):
self. screen.blit(self.image,(self.x,self.y))
for bullet in self.bulletList:
bullet.display()
bullet.move()
if bullet.judge():
self.bulletList.remove(bullet)
class GamePlan(Plan,pygame.sprite.Sprite):
def __init__(self,screen):
self.imageName = "./feiji/hero.gif"
super().__init__(screen)
Sprite.__init__(self) self.rect = self.image.get_rect() self.rect.x=200
self.rect.y=680
#加载我机损毁图片
self.bomb1 = pygame.image.load("./feiji/hero_blowup_n1.png")
self.bomb2 = pygame.image.load("./feiji/hero_blowup_n2.png")
self.bomb3 = pygame.image.load("./feiji/hero_blowup_n3.png")
self.bombList = [self.bomb1,self.bomb2,self.bomb3]
def display(self):
self.screen.blit(self.image,(self.rect.x,self.rect.y))
for bullet in self.bulletList:
bullet.display()
bullet.move()
def moveLeft(self):
if self.rect.x >=0:
self.rect.x-=20
def moveRight(self):
if self.rect.x <480-100:
self.rect.x+=20
def moveUp(self):
if self.rect.y>0:
self.rect.y-=20
def moveDown(self):
if self.rect.y<860-124:
self.rect.y+=20
def _del_(self):
print("游戏结束")
def shootbullet(self):
Newbullet =PublicBullet(self.rect.x,self.rect.y,self.screen)
self.bulletList.append(Newbullet)
bullet_sound.play()
class EnemyPlan(Plan,pygame.sprite.Sprite):
def __init__(self,screen):
self.speed = random.randint(1,3)
self.imageName = "./feiji/enemy-1.gif"
super().__init__(screen)
Sprite.__init__(self)
#确定敌机位置
self.rect = self.image.get_rect()
self.reset()
#加载敌机损毁图片
self.bomb1 = pygame.image.load("./feiji/enemy0_down1.png")
self.bomb2 = pygame.image.load("./feiji/enemy0_down2.png")
self.bomb3 = pygame.image.load("./feiji/enemy0_down3.png")
self.bombList = [self.bomb1,self.bomb2,self.bomb3]
def reset(self): self.rect.x = random.randint(0,400)
self.rect.y= 0 def move(self):
self.rect.y+=self.speed
def update(self):
if self.rect.y>860:
self.kill()
def _del_(self):
print("敌机挂了") class PublicBullet(Base):
def __init__(self,x,y,screen):
super().__init__(screen)
self.imageName="./feiji/bullet-3.gif"
self.x = x+40
self.y = y-20
self.image = pygame.image.load(self.imageName).convert()
def move(self):
self.y-=4 def display(self):
self.screen.blit(self.image,(self.x,self.y))
def judge(self):
if self.y<0 or self.y>860 :
return True
else:
return False #设置敌机精灵组
enemy = EnemyPlan(screen)
enemy1 = EnemyPlan(screen)
enemy2 = EnemyPlan(screen)
enemy3 = EnemyPlan(screen)
enemy4 = EnemyPlan(screen)
enemy5 = EnemyPlan(screen)
enemy6 = EnemyPlan(screen)
enemy7= EnemyPlan(screen)
enemy_group = pygame.sprite.Group(enemy,enemy1,enemy2,enemy3,enemy4,enemy5,enemy6,enemy7) gamePlan =GamePlan(screen)
def key_control(gamePlan):
for event in pygame.event.get():
#退出按钮
if event.type == QUIT:
print("exit")
exit()
elif event.type == CREAT_ENEMY:
enemy = EnemyPlan(screen)
enemy_group.add(enemy)
#按键
elif event.type == KEYDOWN:
if event.key == K_LEFT:
gamePlan.moveLeft()
elif event.key == K_RIGHT:
gamePlan.moveRight()
elif event.key == K_UP:
gamePlan.moveUp()
elif event.key == K_DOWN:
gamePlan.moveDown()
elif event.key == K_SPACE:
gamePlan.shootbullet()
def main():
#创建一个窗口,用来显示内容
screen = pygame.display.set_mode((480,800),0,32)
#创建一个和窗口大小的图片,用来充当背景
background = pygame.image.load("./feiji/background.png").convert()
#
clock = pygame.time.Clock()
pygame.mixer.music.play(-1)
enemy_index = 0
plan_index = 0
score = 0
while True:
#设定需要显示的背景图
screen.blit(background,(0,0))
# 刷新帧率
clock.tick(60)
gamePlan.display() #让精灵组中所有精灵更新位置
enemy_group.update()
enemy_group.draw(screen) for enemy in enemy_group:
enemy.move()
x1 = enemy.rect.x
x2 = enemy.rect.x + 51
y1 = enemy.rect.y
y2 = enemy.rect.y + 39
for bullet in gamePlan.bulletList:
x = bullet.x
y = bullet.y
a = x>x1 and x<x2 and y>y1 and y<y2
if a:
screen.blit(enemy.bombList[enemy_index],enemy.rect)
enemy_index = (enemy_index +1)%3
time.sleep(0.022)
if enemy_index == 0:
enemy_down_sound.play()
enemy.kill()
score +=10
c1 = gamePlan.rect.x
c2 = gamePlan.rect.x + 100
d1 = gamePlan.rect.y
b = c1<x2 and c2>x1 and d1<y2
if b:
screen.blit(enemy.bombList[enemy_index],enemy.rect)
screen.blit(gamePlan.bombList[plan_index],gamePlan.rect)
plan_index = (plan_index +1)%3
time.sleep(0.022)
if plan_index == 0:
me_down_sound.play()
says =("Game Over!")
my_font = pygame.font.SysFont("UbuntuMono-Bold1",84)
says_suface = my_font.render(says,True,(10,10,10))
pygame.image.save(says_suface,"hello.png")
screen.blit(says_suface,(100,300))
say = ("SCORE:%d"%score)
my_font = pygame.font.SysFont("UbuntuMono-Bold1",64)
say_surface = my_font.render(say,True,(0,0,0))
pygame.image.save(say_surface,"12.png")
screen.blit(say_surface,(150,400)) key_control(gamePlan)
pygame.display.update()
if __name__ =="__main__": main()
python飞机大战代码的更多相关文章
- 微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js)
微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞 ...
- 微信小游戏 demo 飞机大战 代码分析 (三)(spirit.js, animation.js)
微信小游戏 demo 飞机大战 代码分析(三)(spirit.js, animation.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码 ...
- 微信小游戏 demo 飞机大战 代码分析 (二)(databus.js)
微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...
- 微信小游戏 demo 飞机大战 代码分析 (一)(game.js, main.js)
微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...
- python版飞机大战代码简易版
# -*- coding:utf-8 -*- import pygame import sys from pygame.locals import * from pygame.font import ...
- Python飞机大战实例有感——pygame如何实现“切歌”以及多曲重奏?
目录 pygame如何实现"切歌"以及多曲重奏? 一.pygame实现切歌 初始化路径 尝试一 尝试二 尝试三 成功 总结 二.如何在python多线程顺序执行的情况下实现音乐和音 ...
- python飞机大战
'''新手刚学python,仿着老师敲的代码.1.敌方飞机只能左右徘徊(不会往下跑)并且不会发射子弹.2.正在研究怎么写计分.3.也参考了不少大佬的代码,但也仅仅只是参考了.加油!''' import ...
- python飞机大战简单实现
小游戏飞机大战的简单代码实现: # 定义敌机类 class Enemy: def restart(self): # 重置敌机的位置和速度 self.x = random.randint(50, 400 ...
- python 飞机大战 实例
飞机大战 #coding=utf-8 import pygame from pygame.locals import * import time import random class Base(ob ...
随机推荐
- bootstrap学习: 折叠插件和面板
bootstrap提供了面板排版工具和折叠插件,能够用来实现新闻列表.留言板.博客分块等: 1.折叠插件: <a data-toggle="collapse" data-ta ...
- 金融量化分析【day113】:聚宽自带策略
一.策略代码 # 导入函数库 from jqdata import * # 初始化函数,设定基准等等 def initialize(context): # 设定沪深300作为基准 set_benchm ...
- linux,pthread(转)
互斥量.条件变量与pthread_cond_wait()函数的使用,详解(二) 1.Linux“线程” 进程与线程之间是有区别的,不过linux内核只提供了轻量进程的支持,未实现线程模型.Linu ...
- Idea 创建控制台程序
1:前提配置好 jdk环境. 最后一步:填写项目名称和保存的路径 即可.
- thinkpad 睡眠唤醒后热键功能正常,但屏幕无法显示状态/进度条/图标
由于博主比较习惯笔记本开盖即用,合盖即走,不大习惯开机关机(毕竟SSD速度杠杠滴^_^).可是发现笔记本长时间睡眠乃至休眠唤醒后,使用thinkpad热键,虽然可以调节,但屏幕不显示调节状态了.解决步 ...
- win10安装mysql5.7.20解压版
mysql安装包可到官网下载,地址:https://dev.mysql.com/downloads/mysql 1.首先解压文件包,我这解压到E:\install_work\mysql目录下: 2.发 ...
- 关于读取excel 和 写excel
def sync_db(data_list): '''Synchron potential student from excel to PrepareToCrm ''' push_list = ...
- 微信支付的安全漏洞之XXE
1.场景:国外安全社区公布微信支付官方SDK存在严重漏洞,可导致商家服务器被入侵(绕过支付的效果).目前,漏洞详细信息以及攻击方式已被公开,影响范围巨大(已确认陌陌.vivo因使用该SDK而存在该漏洞 ...
- vmware安装centOs操作系统配置网络的一系列问题
1:最近公司在测试项目,需要在linux操作系统上面测试,可惜自己之前学linux操作系统不是很深,配置网络也不是很熟练,网上方法太多,但是不是很好用,确实难为了自己一把,在这里自己总结一下配置网络的 ...
- PHP二维数组按某个字段排序
//准备 二维数组 //按一个字段排序 foreach($rank as $key=>$val){ $dos[$key] = $val['timelength']; } array_multis ...