import pygame
import pgzrun
import random
from random import randint
WIDTH=800
HEIGHT=600
balloon=Actor("balloon",(200,300))
bird1=Actor("bird-up",((randint(800,1600),randint(10,200))))
bird2=Actor("bird-up",((randint(800,1600),randint(10,200))))
house=Actor("house",((randint(800,1600),460)))
tree1=Actor("tree",((randint(800,1600),450)))
tree2=Actor("tree",((randint(800,1600),450)))
bird_up1=True
bird_up2=True
up=False
game_over=False
score=0
number_of_updates=0
number_of_updates2=0
scores=[]
def init_game():
global balloon,bird2,bird1,tree2,tree1,house,up,game_over,score,number_of_updates,scores
global bird_up2,bird_up1,number_of_updates2
balloon = Actor("balloon", (200, 300))
bird1 = Actor("bird-up", (randint(800, 1600), randint(10, 200)))
bird2 = Actor("bird-up", (randint(800, 1600), randint(10, 200)))
house = Actor("house", (randint(800, 1600), 460))
tree1 = Actor("tree", (randint(800, 1600), 450))
tree2 = Actor("tree", (randint(800, 1600), 450))
bird_up1 = True
bird_up2 = True
up = False
game_over = False
score = 0
number_of_updates = 0
number_of_updates2=0
scores = []
def update_high_scores():
global score,scores
filename=r"D:\py-game\1\high-score.txt"
scores=[]
flag=False
with open(filename,"r") as file:
line=file.readline()
high_scores=line.split()
for high_score in high_scores:
if(score> int(high_score)):
scores.append(str(score)+" ")
score=int(high_score)
flag=True
else:
scores.append(str(high_score)+" ")
if flag:
with open(filename,"w") as file:
for high_score in scores:
file.write(high_score)
def display_high_scores():
screen.draw.text("HIGH SCORES",(350,150),color="black")
y=175
position=1
for high_score in scores:
screen.draw.text(str(position)+". "+high_score,(350,y),color="black")
y+=25
position+=1
#while(1):
# on_key_up(key)
def draw():
screen.blit("background",(0,0))
if not game_over:
balloon.draw()
bird1.draw()
bird2.draw()
tree1.draw()
tree2.draw()
house.draw()
else:
display_high_scores()
def on_mouse_down():
global up
up=True
balloon.y-=50
def on_mouse_up():
global up
up=False
def on_key_up(key):
global game_over
STEP_Y=30
STEP_X=30
vx,vy=balloon.x,balloon.y
#if (key == keys.UP) or (key == keys.RIGHT) or (key == keys.DOWN) or (key == keys.LEFT):
if key==keys.UP:
balloon.y-=STEP_Y+20
if balloon.top<=0:
balloon.y=vy
game_over = True
if key == keys.DOWN:
balloon.y+=STEP_Y
if balloon.bottom>=560:
balloon.y=vy
game_over=True
if key == keys.RIGHT:
balloon.x+=STEP_X
if balloon.right>=800:
balloon.x=vx
if key == keys.LEFT:
balloon.x-=STEP_X
if balloon.left <=0:
balloon.x = vx
if key==keys.SPACE:
init_game()
def flap1():
global bird_up1,bird1
if bird_up1:
bird1.image="bird-up"
bird_up1=False
else:
bird1.image="bird-down"
bird_up1=True
def flap2():
global bird_up2,bird2
if bird_up2:
bird2.image="bird-up"
bird_up2=False
else:
bird2.image="bird-down"
bird_up2=True
SPEED=2
def update():
global game_over,score,number_of_updates,SPEED,number_of_updates2
if not game_over:
if not up:
balloon.y +=1
if bird1.x>0:
bird1.x-=SPEED*2
if number_of_updates>9:
flap1()
number_of_updates=0
else:
number_of_updates +=1
else:
score += 1
bird1.pos=(randint(800,1600),randint(10,200))
if bird2.x > 0:
bird2.x -= SPEED * 2
if number_of_updates2 > 9:
flap2()
number_of_updates2= 0
else:
number_of_updates2 += 1
else:
score+=1
bird2.pos = (randint(800, 1600), randint(10, 200))
if tree1.x>0:
tree1.x -=SPEED
else:
score += 1
tree1.pos=(randint(800, 1600), 450)
if tree2.x>0:
tree2.x -=SPEED
else:
score += 1
tree2.pos = (randint(800, 1600), 450)
if (tree2.x>=tree1.x-30) and (tree2.x<=tree1.x+30):
tree2.x+=40
if house.x>0:
house.x -=SPEED
else:
score += 1
house.pos=(randint(800, 1600), 460)
SPEED=2+score//10
if balloon.collidepoint(bird1.x,bird1.y) or \
balloon.collidepoint(bird2.x, bird2.y) or \
balloon.collidepoint(tree2.x, tree2.y) or \
balloon.collidepoint(house.x, house.y) or \
balloon.collidepoint(tree1.x, tree1.y):
game_over=True
update_high_scores()
pgzrun.go()
pygame zero 编写气球游戏
26 浏览量
2024-04-29
21:21:31
上传
评论
收藏 91KB ZIP 举报
lanhai_201011
- 粉丝: 32
- 资源: 6