Commit 727e6f74 by BellCodeEditor

save project

parent 6c92741d
Showing with 47 additions and 20 deletions
import pygame
from pygame import locals
import random
import json
pygame.init() # 初始化
class Player(pygame.sprite.Sprite):
class Player(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
class Block(pygame.sprite.Sprite):
class Block(pygame.sprite.Sprite):#障碍物精灵
def __init__(self,image1,image2,image3):
super().__init__()
self.image=random.choice([image1,image2,image3])
......@@ -19,13 +20,13 @@ class Block(pygame.sprite.Sprite):
self.rect.y=500-self.rect.height
self.score=1
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
screen = pygame.display.set_mode((1000, 600))#窗口
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
pygame.display.set_caption("悟空酷跑")
pygame.display.set_caption("悟空酷跑")#窗口标题
# 载入图片
background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路
my_font=pygame.font.Font('STKAITI.TTF',32)
my_font=pygame.font.Font('STKAITI.TTF',32)#字体对象
stone = pygame.image.load('stone.png') # 石头
cacti = pygame.image.load('cacti.png') # 仙人掌
bush = pygame.image.load('bush.png') # 灌木丛
......@@ -33,7 +34,9 @@ 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')]
pygame.image.load('hero5.png')]#悟空造型列表
score_audio=pygame.mixer.Sound('score.wav')#得分音效
old_score=0
road_x=0
bg_x=0
index = 0
......@@ -44,10 +47,15 @@ time=0 #精灵之间的间隔时间
#创建一个障碍物对象
#obstacle = Block(stone,cacti,bush)
block_list=pygame.sprite.Group() #创建一个精灵组
gamestate=True
gamestate=True #游戏状态
score=0
speed=8
with open('record.txt','r',encoding='utf-8')as f:#打开分数记录文件
content=f.read()
record=json.loads(content)#将json数据转化为字典
one=record["第1名"]#通过字典的键取对应的值
two=record["第2名"]
three=record["第3名"]
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
......@@ -84,7 +92,7 @@ while True:
if bg_x<-1000:
bg_x=0
screen.blit(background, (bg_x, 0)) # 远处背景
road_x-=8
road_x-= speed
if road_x<-1000:
road_x=0
screen.blit(road, (road_x, 500)) # 路
......@@ -101,22 +109,35 @@ while True:
obstacle=Block(stone,cacti,bush)
block_list.add(obstacle)
for sprite in block_list:
sprite.rect.x-=8
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 sprite.rect.x<0-sprite.rect.width:#如果障碍物跑出左边界
sprite.kill()#删除这个障碍物
if pygame.sprite.collide_rect(wukong,sprite):#检测悟空和精灵碰撞
gameover=pygame.image.load('gameover.png')
screen.blit(gameover,(400,200))
gamestate=False
screen.blit(gameover,(400,200))#渲染失败画面
gamestate=False#游戏停止
else:
if sprite.rect.x+sprite.rect.width<wukong.rect.x:
if sprite.rect.x+sprite.rect.width<wukong.rect.x:#如果障碍物跑到悟空左边
score+=sprite.score
sprite.score=0
info='分数:'+str(score)
fenshu=my_font.render(info,True,(255,0,0))
screen.blit(fenshu,(800,100))
speed = 8 + score // 3 #每增加3分,速度增加1
if score > old_score:
score_audio.play()
old_score = score
# info='分数:'+str(score)
# fenshu=my_font.render(info,True,(255,0,0))
# screen.blit(fenshu,(800,100))
#分数
info_score=my_font.render('分数:'+str(score),True,(255,255,255))
screen.blit(info_score,(800,20))
info_score=my_font.render('第1名:'+str(one),True,(255,255,255))
screen.blit(info_score,(800,50))
info_score=my_font.render('第2名:'+str(two),True,(255,255,255))
screen.blit(info_score,(800,80))
info_score=my_font.render('第3名:'+str(three),True,(255,255,255))
screen.blit(info_score,(800,110))
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
import json
jsonData='{"第一名":122,"第二名":109,"第三名":100}'
text=json.loads(jsonData)
print('第一名:',text['第一名'])
\ 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