Commit 727e6f74 by BellCodeEditor

save project

parent 6c92741d
Showing with 47 additions and 20 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random import random
import json
pygame.init() # 初始化 pygame.init() # 初始化
class Player(pygame.sprite.Sprite): class Player(pygame.sprite.Sprite):#悟空精灵
def __init__(self,image): def __init__(self,image):
super().__init__() super().__init__()
self.image=image self.image=image
self.rect=self.image.get_rect() self.rect=self.image.get_rect()
self.rect.x=150 self.rect.x=150
self.rect.y=400 self.rect.y=400
class Block(pygame.sprite.Sprite): class Block(pygame.sprite.Sprite):#障碍物精灵
def __init__(self,image1,image2,image3): def __init__(self,image1,image2,image3):
super().__init__() super().__init__()
self.image=random.choice([image1,image2,image3]) self.image=random.choice([image1,image2,image3])
...@@ -19,13 +20,13 @@ class Block(pygame.sprite.Sprite): ...@@ -19,13 +20,13 @@ class Block(pygame.sprite.Sprite):
self.rect.y=500-self.rect.height self.rect.y=500-self.rect.height
self.score=1 self.score=1
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((1000, 600)) screen = pygame.display.set_mode((1000, 600))#窗口
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
pygame.display.set_caption("悟空酷跑") pygame.display.set_caption("悟空酷跑")#窗口标题
# 载入图片 # 载入图片
background = pygame.image.load('bg.png') # 背景 background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.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') # 石头 stone = pygame.image.load('stone.png') # 石头
cacti = pygame.image.load('cacti.png') # 仙人掌 cacti = pygame.image.load('cacti.png') # 仙人掌
bush = pygame.image.load('bush.png') # 灌木丛 bush = pygame.image.load('bush.png') # 灌木丛
...@@ -33,7 +34,9 @@ hero = [pygame.image.load('hero1.png'), ...@@ -33,7 +34,9 @@ hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero2.png'), pygame.image.load('hero2.png'),
pygame.image.load('hero3.png'), pygame.image.load('hero3.png'),
pygame.image.load('hero4.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 road_x=0
bg_x=0 bg_x=0
index = 0 index = 0
...@@ -44,10 +47,15 @@ time=0 #精灵之间的间隔时间 ...@@ -44,10 +47,15 @@ time=0 #精灵之间的间隔时间
#创建一个障碍物对象 #创建一个障碍物对象
#obstacle = Block(stone,cacti,bush) #obstacle = Block(stone,cacti,bush)
block_list=pygame.sprite.Group() #创建一个精灵组 block_list=pygame.sprite.Group() #创建一个精灵组
gamestate=True gamestate=True #游戏状态
score=0 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: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.QUIT: if event.type == locals.QUIT:
...@@ -84,7 +92,7 @@ while True: ...@@ -84,7 +92,7 @@ while True:
if bg_x<-1000: if bg_x<-1000:
bg_x=0 bg_x=0
screen.blit(background, (bg_x, 0)) # 远处背景 screen.blit(background, (bg_x, 0)) # 远处背景
road_x-=8 road_x-= speed
if road_x<-1000: if road_x<-1000:
road_x=0 road_x=0
screen.blit(road, (road_x, 500)) # 路 screen.blit(road, (road_x, 500)) # 路
...@@ -101,22 +109,35 @@ while True: ...@@ -101,22 +109,35 @@ while True:
obstacle=Block(stone,cacti,bush) obstacle=Block(stone,cacti,bush)
block_list.add(obstacle) block_list.add(obstacle)
for sprite in block_list: for sprite in block_list:
sprite.rect.x-=8 sprite.rect.x-=speed #障碍物向左移动
screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y)) screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y))
if sprite.rect.x<0-sprite.rect.width: if sprite.rect.x<0-sprite.rect.width:#如果障碍物跑出左边界
sprite.kill() sprite.kill()#删除这个障碍物
if pygame.sprite.collide_rect(wukong,sprite):#检测悟空和精灵碰撞 if pygame.sprite.collide_rect(wukong,sprite):#检测悟空和精灵碰撞
gameover=pygame.image.load('gameover.png') gameover=pygame.image.load('gameover.png')
screen.blit(gameover,(400,200)) screen.blit(gameover,(400,200))#渲染失败画面
gamestate=False gamestate=False#游戏停止
else: else:
if sprite.rect.x+sprite.rect.width<wukong.rect.x: if sprite.rect.x+sprite.rect.width<wukong.rect.x:#如果障碍物跑到悟空左边
score+=sprite.score score+=sprite.score
sprite.score=0 sprite.score=0
info='分数:'+str(score) speed = 8 + score // 3 #每增加3分,速度增加1
fenshu=my_font.render(info,True,(255,0,0)) if score > old_score:
screen.blit(fenshu,(800,100)) 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() pygame.display.update()
FPS.tick(60) 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