# -*- coding: utf-8 -*-

import pygame
from sys import exit
import random pygame.init()
screen = pygame.display.set_mode((450, 600), 0, 32)
background = pygame.image.load("back.jpg").convert()
plane = pygame.image.load("plane.png").convert_alpha()
pygame.display.set_icon(plane)
pygame.display.set_caption("打飞机") class Enemy:
def restart(self):
#重置敌机位置和速度
self.x = random.randint(50, 400)
self.y = random.randint(-200, -50)
self.speed = random.random() + 0.1 def __init__(self):
#初始化
self.restart()
self.image = pygame.image.load('enemy.png').convert_alpha() def move(self):
if self.y < 600:
#向下移动
self.y += 0.3
else:
#重置
self.restart()
#enemy = Enemy()
enemies = []
for i in range(3):
enemies.append(Enemy()) class Bullet:
def __init__(self):
#初始化成员变量,x,y,image
self.x = 0
self.y = -1
self.image = pygame.image.load('bullet.png').convert_alpha()
self.active = False
def restart(self):
mouseX, mouseY = pygame.mouse.get_pos()
self.x = mouseX - self.image.get_width() / 2
self.y = mouseY - self.image.get_height() /2
self.active = True
def move(self):
#处理子弹的运动
if self.y < 0:
self.active = False
if self.active:
self.y -= 3
#创建子弹的list
bullets = []
#向list中添加5发子弹
for i in range(5):
bullets.append(Bullet())
#子弹总数
count_b = len(bullets)
#即将激活的子弹序号
index_b = 0
#发射子弹的间隔
interval_b = 0 class Plane:
def restart(self):
self.x = 200
self.y = 600 def __init__(self):
self.restart()
self.image = pygame.image.load('plane.png').convert_alpha() def move(self):
x, y = pygame.mouse.get_pos()
x-= self.image.get_width() / 2
y-= self.image.get_height() / 2
self.x = x
self.y = y plane = Plane()
bullet = Bullet()
enemy = Enemy()
def checkHit(enemy, bullet):
if (bullet.x > enemy.x and bullet.x < enemy.x + enemy.image.get_width()) and (bullet.y > enemy.y and bullet.y < enemy.y + enemy.image.get_height()):
enemy.restart()
bullet.active = False
return True
return False def checkCrash(enemy, plane):
if (plane.x + 0.7*plane.image.get_width() > enemy.x) and (plane.x + 0.3*plane.image.get_width() < enemy.x + enemy.image.get_width()) and (plane.y + 0.7*plane.image.get_height() > enemy.y) and (plane.y + 0.3*plane.image.get_height() < enemy.y + enemy.image.get_height()):
return True
return False gameover = False
score = 0
Hscore = []
font = pygame.font.SysFont('楷体', 16)
text1 = font.render(u"点击鼠标左键重新开始", 1, (0, 0, 0)) while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
#判断在gameover状态下点击了鼠标
if gameover and event.type == pygame.MOUSEBUTTONUP:
#重置游戏
plane.restart()
for e in enemies:
e.restart()
for b in bullets:
b.active = False
score = 0
gameover = False
screen.blit(background, (0,0))
if not gameover:
interval_b -= 1
if interval_b < 0:
bullets[index_b].restart()
interval_b = 100
index_b = (index_b+1) % count_b
for b in bullets:
if b.active:
for e in enemies:
if checkHit(e, b):
score += 100
if checkCrash(e, plane):
gameover = True b.move()
screen.blit(b.image, (b.x-23, b.y))
screen.blit(b.image, (b.x+25, b.y))
for e in enemies:
e.move()
screen.blit(e.image, (e.x, e.y))
plane.move()
screen.blit(plane.image, (plane.x, plane.y))
text = font.render("Socre:%d" % score, 1, (0, 0, 0))
screen.blit(text, (0, 0))
else:
screen.blit(text, (0, 0))
screen.blit(text1,(100,32))
Hscore.append(score)
text2 = font.render(u"HScore:%d" % sorted(Hscore)[-1], 1, (0, 0, 0))
screen.blit(text2, (250, 0))
pass
pygame.display.update()

最新文章

  1. ASP.NET SignalR 高可用设计
  2. 拓扑排序&lt;反向拓扑+有向环的判断&gt;
  3. C++:基础篇-32位和64位系统区别及字节数
  4. CentOS 静态IP分配,提示Error, some other host already uses address解决办法
  5. Python之进程
  6. 没有显示器如何SSH连接上树莓派
  7. javascript的数组之push()
  8. TempData ViewBag ViewData区别
  9. ruby--Hash方法汇总
  10. Kubernetes持久化存储2——探究实验
  11. 5.2Python函数(二)
  12. css三种样式表写法
  13. ERROR! The server quit without updating PID file (/application/mysql-5.6.40/data/db01-51.pid).
  14. sqlserver 中常见的函数字符串函数
  15. Again Prime? No Time. UVA - 10780(质因子分解)
  16. Atcoder Grand 006 C-Rabbit Exercise
  17. SpringBoot(三):文件下载
  18. 任务计划程序-Windows2008定时重启
  19. 11.字符串{a,b}的幂集[回溯递归]
  20. php imagecreatetruecolor()方法报未定义错误解决方法

热门文章

  1. error: templates may not be ‘virtual’
  2. Codeforces Round #269 (Div. 2) A,B,C,D
  3. 第二篇 HTML5打包发布IOS APP之苹果开发者账号申请流程
  4. Faster_Rcnn在windows下运行踩坑总结
  5. windows如何搭建redis集群
  6. 企业级应用,如何实现服务化三(dubbo入门案例)
  7. bzoj3417:[POI2013]MOR-Tales of seafaring
  8. eclipse添加tomcat运行环境
  9. hibernate错误总结1
  10. Php对象及对象特性篇