Commit b46cd75d by BellCodeEditor

save project

parent 3bb2e58c
Showing with 34 additions and 9 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random import random
import json
pygame.init() # 初始化 pygame.init() # 初始化
with open('record.txt','r',encoding='utf-8')as file:
content=file.read()
record=json.loads(content)
print(record)
one,two,three=record["第1名"],record["第2名"],record["第3名"]
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__()
...@@ -48,9 +52,11 @@ road_x = 0 ...@@ -48,9 +52,11 @@ road_x = 0
bg_x = 0 bg_x = 0
time = 0 time = 0
gamestate = True gamestate = True
score=0
block_list =pygame.sprite.Group() # 创建精灵组 block_list =pygame.sprite.Group() # 创建精灵组
f=pygame.font.Font('STKAITI.TTF',20)
speed=10
sound=pygame.mixer.Sound('score.wav')
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:
...@@ -91,7 +97,7 @@ while True: ...@@ -91,7 +97,7 @@ while True:
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)) # 道路
...@@ -106,15 +112,33 @@ while True: ...@@ -106,15 +112,33 @@ while True:
obstacle = Block(bush,cacti,stone) obstacle = Block(bush,cacti,stone)
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:
sound.play()
speed=10+score//3
score+=1
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
if score>three:
if score>one:
record['第1名']=score
record['第2名']=one
record['第3名']=two
elif score>two:
record['第2名']=score
record['第3名']=two
else:
record['第3名']=score
record=json.dumps(record,ensure_ascii=False)
with open('record.txt','w',encoding='utf-8')as file:
file.write(record)
word='分数:'+str(score)+' 第一名:'+str(one)+' 第二名:'+str(two)+' 第三名:'+str(three)
text=f.render(word,True,(0,0,0))
screen.blit(text,(0,0))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPS.tick(60) FPS.tick(60)
\ No newline at end of file
{"第1名": 0, "第2名": 0, "第3名": 0} {"第1名": 23, "第2名": 11, "第3名": 0}
\ No newline at end of file \ 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