Commit b150d866 by BellCodeEditor

save project

parent c1e7b32a
Showing with 27 additions and 9 deletions
import pygame
from pygame import locals
import random
import json
pygame.init() # 初始化
......@@ -26,6 +27,10 @@ class Player(pygame.sprite.Sprite): # 悟空
self.rect.x = 150
self.rect.y = 400
with open('record.txt','r',encoding = 'utf-8') as f:
jsondata = f.read()
dictdata = json.loads(jsondata)
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
......@@ -50,6 +55,9 @@ bg_x = 0
time = 0
score = 0
gamestate = True
speed = 8
score_audio = pygame.mixer.Sound('score.wav')
font = pygame.font.Font("STKAITI.TTF",20)
block_list =pygame.sprite.Group() # 创建精灵组
......@@ -89,17 +97,18 @@ while True:
t =30
# 将背景图画上去
bg_x -= 1
bg_x -= speed // 8
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)) # 道路
screen.blit(wukong.image, (150, y)) # 悟空
speed = 8+score // 3
time += 1
if time >= 60: # 创建障碍物精灵
......@@ -109,7 +118,7 @@ while True:
obstacle = Block(bush,cacti,stone)
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()
......@@ -120,11 +129,20 @@ while True:
if sprite.rect.x + sprite.rect.width < wukong.rect.x and sprite.score == 1:
sprite.score = 0
score += 1
print(score)
score_audio.play()
text = '分数:' + str(score)
info = font.render(text,True,(255,255,255))
screen.blit(info,(900,10))
text1 = '分数:' + str(score)
info1 = font.render(text1,True,(255,255,255))
screen.blit(info1,(850,10))
text2 = '第一名:' + str(dictdata['第1名'])
info2 = font.render(text2,True,(255,255,255))
screen.blit(info2,(850,50))
text3 = '第二名:' + str(dictdata['第2名'])
info3 = font.render(text3,True,(255,255,255))
screen.blit(info3,(850,90))
text4 = '第三名:' + str(dictdata['第3名'])
info4 = font.render(text4,True,(255,255,255))
screen.blit(info4,(850,130))
# 刷新画面
pygame.display.update()
......
{"第1名": 0, "第2名": 0, "第3名": 0}
\ No newline at end of file
{"第1名": 100, "第2名": 50, "第3名": 15}
\ 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