Commit 67686519 by BellCodeEditor

save project

parent 523a3866
cg.png

97.8 KB

import pygame import pygame
from pygame import locals from pygame import locals
import random
import json
pygame.init() # 初始化 pygame.init() # 初始化
class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3): #创建子类
super().__init__()
self.image = random.choice([image1,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
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((1000, 600)) screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
pygame.display.set_caption('悟空快跑') #设置窗口标题 pygame.display.set_caption('悟空快跑') #设置窗口标题
# 载入图片 # 载入图片
background = pygame.image.load('bg.png') # 背景 background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路 road = pygame.image.load('road.png') # 路
stone = pygame.image.load('stone.png') # 石头 stone = pygame.image.load('stone.png') # 石头
cacti = pygame.image.load('cacti.png') # 仙人掌 cacti = pygame.image.load('cacti.png') # 仙人掌
apple = pygame.image.load('bush.png') # 灌木丛 bush = pygame.image.load('bush.png') # 灌木丛
hero = [pygame.image.load('hero1.png'), hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero2.png'), pygame.image.load('hero2.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'),]
gameover = pygame.image.load('gameover.png')
chenggong = pygame.image.load('cg.png')
block_list = pygame.sprite.Group()
basic_font = pygame.font.Font('STKAITI.TTF',32)
score_music = pygame.mixer.Sound('score.wav')
#pygame.mixer.music.load('孤勇者.mp3')
pygame.mixer.music.load('西瓜冬瓜哈密瓜(抖音DJ完整版) - 莹酱.mp3')
index = 0 index = 0
y = 400
jumpState = 'run'
t = 30
road_x = 0
bg_x = 0
time = 0
num = 0
speed = 15
kaishi = 'T'
score = 0
#创建障碍物
with open("score.txt","r",encoding="utf-8") as f:
content = f.read()
zhi = json.loads(content)
one = zhi["第1名"]
two = zhi["第2名"]
three = zhi["第3名"]
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:
# 接收到退出事件后退出程序 # 接收到退出事件后退出程序
exit() exit()
if event.type == locals.KEYDOWN: if event.type == locals.KEYDOWN: #判断按下空格
if event.key == locals.KEY if event.key == locals.K_SPACE and jumpState == 'run':
jumpState = 'up'
# 将背景图画上去 if kaishi == 'T':
screen.blit(background, (0, 0)) wukong = player(hero[index])
screen.blit(road, (0, 500)) if jumpState == "up": # 起跳状态
index += 1 if t > 0:
if index == 5: y -= t
index = 0 wukong.rect.y = y
screen.blit(hero[int(index)], (150, 400)) t -= 2
# 刷新画面 else:
jumpState = "down"
if jumpState == "down": # 降落状态
if t <= 30:
y += t
wukong.rect.y = y
t += 2
else:
jumpState = "run"
t =30
bg_x -= 5
if bg_x <= -1000:
bg_x = 0
screen.blit(background,(bg_x,0))
road_x -= speed
if road_x <= -1000:
road_x = 0
screen.blit(road,(road_x,500))
if jumpState == 'run':
index += 1
if index >= 4:
index = 0
screen.blit(hero[index],(150,y))
speed = 15 + score//10
time += 1
if time >= 60:
time = 0
num = random.randint(0,50)
if num >=20:
obstacle = Block(bush,stone,cacti)
block_list.add(obstacle)
for sprite in block_list:
sprite.rect.x -= speed
screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y))
if sprite.rect.x <= 0 - sprite.rect.width:
sprite.kill()
if pygame.sprite.collide_rect(wukong,sprite):
screen.blit(gameover, (400,200))
kaishi = 'F'
else:
if sprite.rect.x + sprite.rect.width < 150:
score += sprite.score
sprite.score = 0
score_music.play()
scoreSurf = basic_font.render("分数:"+str(score),True,(255,0,0))
screen.blit(scoreSurf,(850,20))
if pygame.mixer.music.get_busy() == False:
pygame.mixer.music.play()
if kaishi == 'F':
pygame.mixer.music.pause()
if score >= 20:
kaishi = 'F'
screen.blit(chenggong,(250,50))
yi = basic_font.render("第1名:" + str(one),True,(0,0,0))
er = basic_font.render("第2名:" + str(two),True,(0,0,0))
san = basic_font.render("第3名:" + str(three),True,(0,0,0))
screen.blit(yi,(850,100))
screen.blit(er,(850,150))
screen.blit(san,(850,200))
# 刷新画面
pygame.display.update() pygame.display.update()
FPS.tick(30) FPS.tick(60)
\ No newline at end of file \ No newline at end of file
File added
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