import pygame
import sys
import random
from math import *
pygame.init()
width = 500
height = 500
display = pygame.display.set_mode((width, height))
pygame.display.set_caption("Blast Off Balloon Game")
clock = pygame.time.Clock()
margin = 100
lowerBound = 100
score = 0
white = (230, 230, 230)
lightBlue = (174, 214, 241)
red = (231, 76, 60)
lightGreen = (25, 111, 61)
darkGray = (40, 55, 71)
skyBlue = (135,206,235)
darkBlue = (21, 67, 96)
green = (35, 155, 86)
yellow = (244, 208, 63)
blue = (46, 134, 193)
purple = (155, 89, 182)
orange = (243, 156, 18)
black = (0, 0, 0)
font = pygame.font.SysFont("Arial", 25)
class Balloon:
def __init__(self, speed):
self.a = random.randint(30, 40)
self.b = self.a + random.randint(0, 10)
self.x = random.randrange(margin, width - self.a - margin)
self.y = height - lowerBound
self.angle = 90
self.speed = -speed
self.probPool = [-1, -1, -1, 0, 0, 0, 0, 1, 1, 1]
self.length = random.randint(50, 100)
self.color = random.choice([red, green, purple, orange, yellow, blue])
def move(self):
direct = random.choice(self.probPool)
if direct == -1:
self.angle += -10
elif direct == 0:
self.angle += 0
else:
self.angle += 10
self.y += self.speed*sin(radians(self.angle))
self.x += self.speed*cos(radians(self.angle))
if (self.x + self.a > width) or (self.x < 0):
if self.y > height/5:
self.x -= self.speed*cos(radians(self.angle))
else:
self.reset()
if self.y + self.b < 0 or self.y > height + 30:
self.reset()
def show(self):
pygame.draw.line(display, darkBlue, (self.x + self.a/2, self.y + self.b), (self.x + self.a/2, self.y + self.b + self.length))
pygame.draw.ellipse(display, self.color, (self.x, self.y, self.a, self.b))
pygame.draw.ellipse(display, self.color, (self.x + self.a/2 - 5, self.y + self.b - 3, 10, 10))
def burst(self):
global score
pos = pygame.mouse.get_pos()
if onBalloon(self.x, self.y, self.a, self.b, pos):
score += 1
self.reset()
def reset(self):
self.a = random.randint(30, 40)
self.b = self.a + random.randint(0, 10)
self.x = random.randrange(margin, width - self.a - margin)
self.y = height - lowerBound
self.angle = 90
self.speed -= 0.002
self.probPool = [-1, -1, -1, 0, 0, 0, 0, 1, 1, 1]
self.length = random.randint(50, 100)
self.color = random.choice([red, green, purple, orange, yellow, blue])
balloons = []
noBalloon = 10
for i in range(noBalloon):
obj = Balloon(random.choice([1, 1, 2, 2, 2, 2, 3, 3, 3, 4]))
balloons.append(obj)
def onBalloon(x, y, a, b, pos):
if (x < pos[0] < x + a) and (y < pos[1] < y + b):
return True
else:
return False
def pointer():
pos = pygame.mouse.get_pos()
r = 25
l = 20
color = lightGreen
for i in range(noBalloon):
if onBalloon(balloons[i].x, balloons[i].y, balloons[i].a, balloons[i].b, pos):
color = black
pygame.draw.ellipse(display, color, (pos[0] - r/2, pos[1] - r/2, r, r), 4)
pygame.draw.line(display, color, (pos[0], pos[1] - l/2), (pos[0], pos[1] - l), 4)
pygame.draw.line(display, color, (pos[0] + l/2, pos[1]), (pos[0] + l, pos[1]), 4)
pygame.draw.line(display, color, (pos[0], pos[1] + l/2), (pos[0], pos[1] + l), 4)
pygame.draw.line(display, color, (pos[0] - l/2, pos[1]), (pos[0] - l, pos[1]), 4)
def lowerPlatform():
pygame.draw.rect(display, red, (0, height - 50, width, 50))
def showScore():
scoreText = font.render("SCORE : " + str(score), True, white)
display.blit(scoreText, (0, height - lowerBound + 50))
def close():
pygame.quit()
sys.exit()
def game():
global score
loop = True
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
close()
if event.key == pygame.K_r:
score = 0
game()
if event.type == pygame.MOUSEBUTTONDOWN:
for i in range(noBalloon):
balloons[i].burst()
display.fill(white)
for i in range(noBalloon):
balloons[i].show()
pointer()
for i in range(noBalloon):
balloons[i].move()
lowerPlatform()
showScore()
pygame.display.update()
clock.tick(60)
game()


新华
- 粉丝: 1w+
最新资源
- 生产流水线小车的PLC控制设计.doc
- 智慧交通产品总体解决方案-交通信息资源平台.docx
- 仓库管理系统设计与实现软件工程课程设计.doc
- Packet-Tracer-5.2实验(十四)-网络地址转换NAT配置.doc
- 电子商务企业电子商务的创建与管理.doc
- 健康养生网站分析推广.ppt
- 幻灯片1首页《数据库原理及其应用》精品课程河南科技大学.ppt
- XXX云计算平台建设总体技术实施方案.doc
- 基于云计算辅助教学的艺术类高职公共英语教学改革与发展研究.docx
- plc电梯毕业-设计.doc
- 翻转课程在计算机基础应用课程中的应用研究.docx
- EPP模式的数据采集卡设计方案.doc
- 液晶显示屏LCD显示接口方案设计书-课程方案设计书.doc
- 项目安全生产文明施工管理网络.doc
- 人事管理系统的研究设计数据库课程研究设计.doc
- 信息系统项目管理师九大知识领域过程输入输出.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


