Commit 8717a336 by BellCodeEditor

auto save

parent 7146e481
File added
bg.png

200 KB

bush.png

14.5 KB

cacti.png

5.58 KB

hero1.png

12.4 KB

hero2.png

10.9 KB

hero3.png

11.3 KB

hero4.png

11.5 KB

hero5.png

11.9 KB

import pygame
from pygame import locals
import json
pygame.init() # 初始化
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
# 载入图片
background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路
stone = pygame.image.load('stone.png') # 石头
cacti = pygame.image.load('cacti.png') # 仙人掌
apple = pygame.image.load('bush.png') # 灌木丛
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')]
index = 0
G=5
y=400
IM='running'
from pygame import locals
import random
from random import *
pygame.init() # 初始化
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
# 载入图片
bg = pygame.image.load('bg.png')
go = pygame.image.load('gameover.png') # 背景
road = pygame.image.load('road.png') # 路
stone = pygame.image.load('stone.png') # 石头
cacti = pygame.image.load('cacti.png') # 仙人掌
apple = pygame.image.load('bush.png') # 灌木丛
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')]
a_font=pygame.font.Font('STKAITI.TTF',18)
s_v=pygame.mixer.Sound('score.wav')
index = 0
G=40
y=400
IM='running'
I_M=0
rond_x=0
bg_x=0
ind=0
dw,s=150,0
time=0
PS=60
gametate=True
yy=0
yyy=8
l=0
score=0
speed=8
os=score
killer=False
p=0
class Rsc(pygame.sprite.Sprite):
def __init__(self,im1,im2,im3):
super().__init__()
self.image=choice([im1,im2,im3])
self.rect=self.image.get_rect()
self.rect.x=1000
self.rect.y=500-self.rect.height
self.score=1
class heros(pygame.sprite.Sprite):
def __init__(self,hero):
super().__init__()
self.image=hero
self.rect=self.image.get_rect()
self.rect.x=150
self.rect.y=400
obg=Rsc(stone,cacti,apple)
wukong=heros(hero[index])
blist=pygame.sprite.Group()
while True:
with open('record.txt','r',encoding='utf-8') as f:
cont=f.read()
text=json.loads(cont)
print(cont)
one=text['第1名']
two=text['第2名']
three=text['第3名']
wukong=heros(hero[index])
if gametate==True:
time+=1
if time>=60:
a=randint(0,65)
if a>=20:
obg=Rsc(stone,cacti,apple)
blist.add(obg)
time=10
s_c=score//3
speed=8+s_c+p
if IM=='running':
ind+=1
if ind>=5:
index+=1
ind=0
if index==5:
index=0
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_SPACE:
if IM=='running':
I_M=0
if IM!='up'and IM!='down':
IM='up'
I_M=1
if IM!='running' and I_M>=1:
if event.key == locals.K_w:
IM='dup'
if event.key == locals.K_s:
gametate=True
if event.key == locals.K_q:
killer=True
if event.key == locals.K_e:
killer=False
if event.key == locals.K_p:
p+=10
if event.key == locals.K_o:
p-=10
if event.key == locals.K_u:
obg=Rsc(stone,cacti,apple)
blist.add(obg)
if gametate==True:
if IM=='up':
y-=G
wukong.rect.y=y
G-=2
if G<= -42:
IM='running'
G=40
if IM=='dup':
wukong.rect.y=75
# if IM=='down':
# if G==0:
# y+=G
# G+=2
# elif G>=0:
# y+=G
# G+=2
# if IM=='dup':
# if G>=0:
# y-=G
# G-=0.15
# else:
# IM='down'
herorun=hero[index]
# 将背景图画上去
bg_x-=speed//4
if bg_x<=-1000:
bg_x=0
screen.blit(bg, (bg_x, 0))
rond_x-=speed
if rond_x<= -1000:
rond_x=0
screen.blit(road, (rond_x, 500))
screen.blit(wukong.image, (150, y))
# if obg.rect.x<0-obg.rect.height:
# obg=Rsc(stone,cacti,apple)
for pop in blist:
pop.rect.x-=speed
screen.blit(pop.image,(pop.rect.x,pop.rect.y))
if pop.rect.x<=0-pop.rect.width:
pop.kill()
if pygame.sprite.collide_rect(wukong,pop):
if killer==True:
pop.kill()
score+=pop.score
speed+=pop.score
pop.score=0
else:
screen.blit(go,(400,200))
gametate=False
else:
if (pop.rect.x+pop.rect.width)<=wukong.rect.x:
score+=pop.score
speed+=pop.score
pop.score=0
if os<score:
s_v.play()
os=score
sF=a_font.render("分数"+str(score),True,(255,255,255))
screen.blit(sF,(880,20))
sF=a_font.render("第一名"+str(one),True,(255,255,255))
screen.blit(sF,(880,40))
sF=a_font.render("第二名"+str(two),True,(255,255,255))
screen.blit(sF,(880,60))
sF=a_font.render("第三名"+str(three),True,(255,255,255))
screen.blit(sF,(880,80))
# 刷新画面
pygame.display.update()
FPS.tick(PS)
\ No newline at end of file
{"第1名": 0, "第2名": 0, "第3名": 0}
\ No newline at end of file
road.png

34.7 KB

File added
stone.png

11.3 KB

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