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. CMDB服务器管理系统【s5day89】:深入理解Java的接口和抽象类

    对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初学的时候会以为它们可以随意互换使用, ...

  2. spring boot打包

    1.application继承SpringBootServletInitializer  重写configure方法如下图 2.去掉项目中多余的main方法 3.将pom.xml打包改为war包 4. ...

  3. ext.net单元格内容换行显示

    增加style .x-grid3-cell-inner {     white-space: normal; }

  4. Select文字居右显示

    使用text-align: right;不起作用,应当使用direction: rtl;

  5. mysql root password

    """centos:mysql忘记root密码解决 1.修改MySQL的登录设置: # vim /etc/my.cnf 在[mysqld]的段中加上一句:skip-gra ...

  6. 支持动态调频_配置AXP228电源管理_4核8核兼容设计_iTOP-4418/6818开发板

    iTOP-4418/6818开发板 支持动态调频,AXP228电源管理, 系统支持:Android4.4/5.1.1.Linux3.4.39.QT2.2/4.7/5.7.Ubuntu12.04 内存: ...

  7. RT throttling分析【转】

    转自:https://blog.csdn.net/u012728256/article/details/72639612 Linux上调度策略为SCHED_FIFO的实时进程是根据优先级抢占运行的.当 ...

  8. vim简单使用教程【转】

    vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...

  9. 关于TabLayout与ViewPager在Fragment中嵌套Fragment使用或配合使用的思考

    注意: 因为继承的是Fragment,所以getSupportFragmentManager()与getFragmentManager()方法无法使用,这里需要用到getChildFragmentMa ...

  10. C# 微信开发-----微信会员卡(二)

    主要说说如何使用微信的激活会员卡 如图: 点击激活会员卡时,要跳转到如下的图片: 要实现这个功能,首先我们在创建会员卡后就操作如下代码 #region 添加激活时的自定义字段 string custo ...