Commit 40302613 by BellCodeEditor

save project

parent 7ba77db5
Showing with 107 additions and 29 deletions
import pygame
from pygame import locals
import random
import json
class Block(pygame.sprite.Sprite):
def __init__ (self,image1,image2,image3):
super().__init__()
self.image=random.choice([image1,image2,image3])
self.rect=self.image.get_rect()
self.rect.x=1000
self.rect.y=500-self.rect.height
self.score = 1
class Wukong(pygame.sprite.Sprite):
def __init__ (self,image):
super().__init__()
self.image=image
self.rect=self.image.get_rect()
self.rect.x=150
self.rect.y=400
pygame.init() # 初始化
......@@ -20,54 +37,117 @@ hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero3.png'),
pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')]
gameover = pygame.image.load('gameover.png')
my_font = pygame.font.Font("STKAITI.TTF",32)
score_audio = pygame.mixer.Sound("score.wav")
shan_x = 0
lu_x = 0
my_sprite = pygame.sprite.Group()
zaoxin = 0
wukongstate = "run"
wukong_y = 400
speed = 8
y = 30
score = 0
gamestate = "True"
time = 0
obstacle = [stone,cacti,bush]
with open("record.txt","r",encoding="utf-8") as f:
content = f.read()
record = json.loads(content)
one = record["第1名"]
two = record["第2名"]
three = record["第3名"]
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_SPACE:
if wukongstate == "run":
if event.key == locals.K_SPACE:
wukongstate = "up"
speed = 8+score//3
if gamestate == "True":
wukong = Wukong(hero[zaoxin])
if wukongstate == "up": # 起跳状态
if y >= 0:
wukong_y -= y
wukong.rect.y = wukong_y
y -= 2
else:
wukongstate = "down"
if wukongstate == "down":
if wukong_y > 150:
if wukongstate == "down": # 下落状态
if y <= 30:
wukong_y += y
y -= 5
else:
wukongstate == "up"
y = 30
if wukongstate == "up":
if wukong_y < 400:
wukong_y -= y
y += 5
wukong.rect.y = wukong_y
y += 2
else:
wukongstate == "run"
wukongstate = "run"
y = 30
if wukongstate == "run":
zaoxin += 1 # 切换造型
if zaoxin == 5:
zaoxin = 0
shan_x -= 2
if shan_x < -1000:
shan_x=0
screen.blit(background, (shan_x, 0))
shan_x -= speed # 渲染背景
if shan_x < -1000:
shan_x=0
screen.blit(background, (shan_x, 0))
lu_x -= 4
if lu_x < -1000:
lu_x=0
screen.blit(road, (lu_x, 500))
zaoxin += 1
if zaoxin == 5:
zaoxin = 0
screen.blit(hero[zaoxin],(100,wukong_y))
lu_x -= speed # 渲染道路
if lu_x < -1000:
lu_x=0
screen.blit(road, (lu_x, 500))
screen.blit(wukong.image, (150,wukong_y))
time += 1
if time >= 60:
time = 0
num = random.randint(0,50)
if num >= 20:
obstacle = Block(stone,cacti,bush)
my_sprite.add(obstacle)
for sprite in my_sprite:
sprite.rect.x -= speed
screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y))
if sprite.rect.x < 0-sprite.rect.width:
sprite.kill()
if pygame.sprite.collide_rect(wukong,sprite):
screen.blit(gameover,(400,200))
gamestate = "Flase"
if score > one:
record["第一名"] = score
record["第二名"] = one
record["第三名"] = two
elif score > two:
record["第二名"] = score
record["第三名"] = two
elif score > three:
record["第三名"] = score
record = json.dumps(record,ensure_ascii=False)
with open("record.txt","w",encoding="utf-8") as s:
s.write()
else:
if sprite.rect.x + sprite.rect.x < wukong.rect.x: # 渲染分数
score += sprite.score
sprite.score = 0
score_audio.play()
score_sprite = my_font.render("分数"+str(score),True,(0,0,0))
yi = my_font.render("第一名"+str(one),True,(0,0,0))
er = my_font.render("第二名"+str(two),True,(0,0,0))
san = my_font.render("第三名"+str(three),True,(0,0,0))
screen.blit(score_sprite,(900,0))
screen.blit(yi,(870,30))
screen.blit(er,(870,60))
screen.blit(san,(870,90))
# 刷新画面
pygame.display.update()
......
{"第1名": 0, "第2名": 0, "第3名": 0}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment