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飞机大战代码的更多相关文章

  1. 微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js)

    微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞 ...

  2. 微信小游戏 demo 飞机大战 代码分析 (三)(spirit.js, animation.js)

    微信小游戏 demo 飞机大战 代码分析(三)(spirit.js, animation.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码 ...

  3. 微信小游戏 demo 飞机大战 代码分析 (二)(databus.js)

    微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...

  4. 微信小游戏 demo 飞机大战 代码分析 (一)(game.js, main.js)

    微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...

  5. python版飞机大战代码简易版

    # -*- coding:utf-8 -*- import pygame import sys from pygame.locals import * from pygame.font import ...

  6. Python飞机大战实例有感——pygame如何实现“切歌”以及多曲重奏?

    目录 pygame如何实现"切歌"以及多曲重奏? 一.pygame实现切歌 初始化路径 尝试一 尝试二 尝试三 成功 总结 二.如何在python多线程顺序执行的情况下实现音乐和音 ...

  7. python飞机大战

    '''新手刚学python,仿着老师敲的代码.1.敌方飞机只能左右徘徊(不会往下跑)并且不会发射子弹.2.正在研究怎么写计分.3.也参考了不少大佬的代码,但也仅仅只是参考了.加油!''' import ...

  8. python飞机大战简单实现

    小游戏飞机大战的简单代码实现: # 定义敌机类 class Enemy: def restart(self): # 重置敌机的位置和速度 self.x = random.randint(50, 400 ...

  9. python 飞机大战 实例

    飞机大战 #coding=utf-8 import pygame from pygame.locals import * import time import random class Base(ob ...

随机推荐

  1. lombok系列(一)

    如果在类上面使用@Builder注解, @Builder public class A { } controller中使用: public String test(@RequestBody A a){ ...

  2. Matlab中hold on与hold off的用法

    摘录自:https://blog.csdn.net/smf0504/article/details/51830963 https://www.cnblogs.com/shuqingstudy/p/48 ...

  3. 第十一节:基于MVC5+Spring.Net+EF+Log4net 传统的一种搭建模式

  4. nginx反向代理-解决前端跨域问题

    1.定义 跨域是指a页面想获取b页面资源,如果a.b页面的协议.域名.端口.子域名不同,所进行的访问行动都是跨域的,而浏览器为了安全问题一般都限制了跨域访问,也就是不允许跨域请求资源.注意:跨域限制访 ...

  5. 阿里巴巴图标库iconfont上传svg后,显示不了图片

    AI里面选中图形,点对象-路径-轮廓化描边

  6. A fine property of the non-empty countable dense-in-self set in the real line

    A fine property of the non-empty countable dense-in-self set in the real line   Zujin Zhang School o ...

  7. 程序设计-理解java继承-遁地龙卷风

    (0)写在前面 编程和现实世界是息息相关的,你是如何理解现实世界中的继承呢? 继承在编程中应理解为:对父类资源(本文只讨论方法)的使用,父类方法只提供类基本的功能,父类方法之间不存在调用关系. (1) ...

  8. SQL Server - AS

    AS 是给现有的字段名/表名指定一个别名的意思.

  9. python中的赋值操作

    参考:https://www.cnblogs.com/andywenzhi/p/7453374.html?tdsourcetag=s_pcqq_aiomsg(写的蛮好) python中的赋值操作“=” ...

  10. how2heap学习笔记

    github源代码地址 这里只分析glibc2.25堆分配的特性,为了方便调试编译时使用 gcc -g -no-pie <input_file_name> -o <output_fi ...