Commit 806ef0c8 by BellCodeEditor

save project

parent 7146e481
File added
bg.png

200 KB

bush.png

14.5 KB

cacti.png

5.58 KB

hero1.png

12.4 KB

hero2.png

10.9 KB

hero3.png

11.3 KB

hero4.png

11.5 KB

hero5.png

11.9 KB

import pygame
from pygame import locals
import random
import json
screen=pygame.display.set_mode((1000,600))
FPS=pygame.time.Clock()
pygame.display.set_caption("悟空酷跑")
gameover = pygame.image.load('gameover.png')
background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路
stone = pygame.image.load('stone.png') # 石头
cacti = pygame.image.load('cacti.png') # 仙人掌
bush = pygame.image.load('bush.png') # 灌木丛
hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero2.png'),
pygame.image.load('hero3.png'),
pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')]
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
score=0
index = 0
y = 400
time=0
t=30
jumpstate="run"
lu=road.get_rect()
lu_x=0
bg_x=0
gamestate= "True"
jingling_list=pygame.sprite.Group()
with open("record.txt","r",encoding="utf-8") as f:
content=f.read()
reader=json.loads()
one=reader["第一名"]
two=reader["第二名"]
three=reader["第三名"]
#my_font=pygame.font.Font('STKAITI.TTF',18)
while True:
for event in pygame.event.get(): #检测如果按下退出,就结束
if event.type == locals.QUIT:
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_UP:
if jumpstate == "run":
jumpstate="up"
if gamestate == "True":
bg_x-=8 #背景向左移动,到屏幕边缘返回原点
screen.blit(background,(bg_x,0))
if bg_x<=-1000:
bg_x=0
lu_x-=8
screen.blit(road,(lu_x,500))
if lu_x<=-1000:
lu_x=0
wukong=Wukong(hero[index])
if jumpstate == "up":
if t>=0:
y-=t
wukong.rect.y=y
t-=2
else:
jumpstate="down"
if jumpstate == "down":
if t<=30:
y+=t
wukong.rect.y=y
t+=2
else:
jumpstate="run"
t=30
if jumpstate == "run":
index+=1
if index>=4:
index=0
screen.blit(wukong.image,(wukong.rect.x,wukong.rect.y))
time+=1
if time>=60:
time=0
yun=random.randint(0,50)
if yun>10:
obstacle=Block(stone,cacti,bush)
jingling_list.add(obstacle)
for sprite in jingling_list:
sprite.rect.x-=8
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,(300,200))
gamestate = "False"
if score>one:
reader["第一名"]=score
reader["第二名"]=one
reader["第三名"]=two
elif score>two:
reader["第二名"]=score
reader["第三名"]=two
elif score>three:
reader["第三名"]=score
reader=json.dumps(reader,ensure_ascii=False)
with open("record.txt","w",encoding="utf-8") as s:
s.write()
else:
if wukong.rect.x>sprite.rect.x+sprite.rect.width:
score+=sprite.score
sprite.score=0
#fenshu=my_font.render("分数"+score,True,(255,255,255))
#screen.blit(fenshu,(900,0))
first=my_font.render("第一名分数"+str(one),True,(255,255,255))
screen.blit(first,(900,50))
second=my_font.render("第二名分数"+str(two),True,(255,255,255))
screen.blit(second,(900,100))
third=my_font.render("第三名分数"+str(three),True,(255,255,255))
screen.blit(third,(900,150))
# 刷新画面
pygame.display.update()
FPS.tick(60)
{"第1名": 0, "第2名": 0, "第3名": 0}
\ No newline at end of file
road.png

34.7 KB

File added
stone.png

11.3 KB

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