Commit 622c39eb by BellCodeEditor

save project

parent 2efe68b4
Showing with 40 additions and 12 deletions
File added
import pygame import pygame
import random import random
from pygame import locals from pygame import locals
import json
class Block(pygame.sprite.Sprite): class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3): def __init__(self,image1,image2,image3):
...@@ -9,6 +10,7 @@ class Block(pygame.sprite.Sprite): ...@@ -9,6 +10,7 @@ class Block(pygame.sprite.Sprite):
self.rect=self.image.get_rect() self.rect=self.image.get_rect()
self.rect.x=1000 self.rect.x=1000
self.rect.y=500-self.rect.height self.rect.y=500-self.rect.height
self.sceor= 1
class Player(pygame.sprite.Sprite): class Player(pygame.sprite.Sprite):
def __init__(self,image): def __init__(self,image):
...@@ -34,22 +36,33 @@ hero =[pygame.image.load('hero1.png'), ...@@ -34,22 +36,33 @@ hero =[pygame.image.load('hero1.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')]
b=pygame.font.Font('STKAITI.TTF',18)
a=pygame.mixer.Sound('score.wav')
index = 0 index = 0
jf=0 old_sceor=0
sceor=0
gamestat=True gamestat=True
speed=8
road_x=0 road_x=0
time=0 time=0
background_x=0 background_x=0
t=35 t=36
jumpstat='running' jumpstat='running'
y=400 y=400
obstacle=Block(stone,apple,cacti) obstacle=Block(stone,apple,cacti)
Block_list=pygame.sprite.Group() Block_list=pygame.sprite.Group()
with open('record.txt','r',encoding='utf-8') as f:
while True: content=f.read()
recoud=json.loads(content)
one=recoud['第1名']
two=recoud['第2名']
three=recoud['第3名']
while True:
speed=8+sceor//3
wukong=Player(hero[index]) wukong=Player(hero[index])
if jumpstat == 'running': if jumpstat == 'running':
index+=1 index+=1
...@@ -65,7 +78,7 @@ while True: ...@@ -65,7 +78,7 @@ while True:
exit() exit()
if gamestat==True: if gamestat==True:
road_x-=8 road_x-=speed
if road_x<=-1000:#小路移动 if road_x<=-1000:#小路移动
road_x=0 road_x=0
...@@ -81,13 +94,13 @@ while True: ...@@ -81,13 +94,13 @@ while True:
jumpstat='jumpdown' jumpstat='jumpdown'
if jumpstat=='jumpdown': if jumpstat=='jumpdown':
if t<=35: if t<=36:
y+=t y+=t
wukong.rect.y=y wukong.rect.y=y
t+=2 t+=2
else: else:
jumpstat='running' jumpstat='running'
t=35 t=36
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (background_x, 0)) screen.blit(background, (background_x, 0))
...@@ -103,7 +116,7 @@ while True: ...@@ -103,7 +116,7 @@ while True:
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()
...@@ -112,9 +125,24 @@ while True: ...@@ -112,9 +125,24 @@ while True:
screen.blit(gameover,(400,150)) screen.blit(gameover,(400,150))
gamestat=False gamestat=False
else: else:
if if sprite.rect.x+sprite.rect.width<wukong.rect.x:
sceor+=sprite.sceor
sprite.sceor=0
old_sceor=sceor
if sceor>old_sceor:
a.Play()
old_sceor=sceor
score=b.render('分数'+str(sceor),True,(0,0,0))
screen.blit(score,(880,20))
score1=b.render('第一名'+str(one),True,(0,0,0))
score2=b.render('第二名'+str(two),True,(0,0,0))
score3=b.render('第三名'+str(three),True,(0,0,0))
screen.blit(score1,(880,40))
screen.blit(score2,(880,60))
screen.blit(score3,(880,80))
screen.blit(obstacle.image,(obstacle.rect.x,obstacle.rect.y))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPS.tick(60) FPS.tick(90)
\ 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