Commit 76a6ab5f by BellCodeEditor

save project

parent 28bdbe8f
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

left.png

2.07 KB

import pygame
from pygame import locals
import random
from json import *
pygame.init() # 初始化
# 创建一个窗口
pygame.mixer.music.load('score.wav')
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
pygame.display.set_caption('悟空跑酷')
# 载入图片
background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路
stone = pygame.image.load('stone.png') # 石头
cacti = pygame.image.load('cacti.png') # 仙人掌
# cacti = pygame.image.load('play.png')
# apple = pygame.image.load('play.png') # 灌木丛
apple = pygame.image.load('bush.png')
# apple = pygame.image.load('w.png')
# apple = pygame.image.load('left.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')]
gameover=pygame.image.load('gameover.png')
index = 0
defen=0
ziti = pygame.font.Font('ziti.ttf',20)
y=400
class Block(pygame.sprite.Sprite):
def __init__(self,imag1,image2,image3):
super().__init__()
self.image = random.choice([imag1,image2,image3])
self.rect = self.image.get_rect()
self.rect.x =1000
self.rect.y = 500 - self.rect.height
self.score = 1
class Player(pygame.sprite.Sprite):
def __init__(self,image):
super().__init__()
self.image = image
self.rect = self.image.get_rect()
self.rect.x =150
self.rect.y = 400
# zaw=Block(stone,cacti,apple)
road_x=0
bg_x=0
zt='run'
speed = 8
old_defen = defen
time=0
with open('score.txt','r',encoding='utf-8') as scorejson:
duqu1 = scorejson.read()
duqu2 = loads(duqu1)
one = duqu2["第1名"]
two = duqu2["第2名"]
three = duqu2["第3名"]
block_list = pygame.sprite.Group()
gamezt=True
while True:
if defen > old_defen:
pygame.mixer.music.play()
old_defen = defen
speed = 8+defen//3
time+=1
if time>60:
time=0
if random.randint(1,50)>20:
block_list.add(Block(stone,cacti,apple))
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN:
if zt=='run':
if event.key == locals.K_SPACE:
t=30
zt='up'
wukong.rect.y=y
# if event.key == locals.K_SPACE:
# zt='up'
# t=30
if gamezt==True:
wukong=Player(hero[index])
if zt=='up':
if t>0:
y-=t
t-=2
wukong.rect.y=y
else:
zt='down'
if zt=='down':
if t<=30:
y+=t
wukong.rect.y=y
t+=2
else:
zt='run'
# 将背景图画上去
screen.blit(background, (bg_x, 0))
screen.blit(road, (road_x, 500))
text = ziti.render('得分:'+str(defen),True,(255,255,255))
screen.blit(text,(850,10))
screen.blit(wukong.image, (150, y))
if zt=='run':
index=(index+1) % len(hero)
# screen.blit(zaw.image, (zaw.rect.x, zaw.rect.y))
for zaw in block_list:
zaw.rect.x-=speed
if zaw.rect.x<0-zaw.rect.width:
zaw.kill()
screen.blit(zaw.image, (zaw.rect.x, zaw.rect.y))
if pygame.sprite.collide_rect(wukong,zaw):
screen.blit(gameover,(400,200))
gamezt=False
else:
if zaw.rect.x + zaw.rect.width < wukong.rect.x:
defen += zaw.score
zaw.score = 0
road_x-=speed
bg_x-=5
if road_x<=-1000:
road_x=0
if bg_x<=-1000:
bg_x=0
text2 = ziti.render('第一名:'+str(one),True,(255,255,255))
screen.blit(text2,(850,30))
text3 = ziti.render('第二名:'+str(two),True,(255,255,255))
screen.blit(text3,(850,50))
text4 = ziti.render('第三名:'+str(three),True,(255,255,255))
screen.blit(text4,(850,70))
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
play.png

12.1 KB

{"第1名": 0, "第2名": 0, "第3名": 0}
\ No newline at end of file
road.png

34.7 KB

{"第1名": 0, "第2名": 0, "第3名": 0}
\ No newline at end of file
File added
File added
stone.png

11.3 KB

w.png

2.08 KB

File added
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