Commit 152c0897 by BellCodeEditor

auto save

parent 7eae44f5
Showing with 164 additions and 124 deletions
File added
import pygame import pygame
from pygame import locals from pygame import locals
import random import random
import json
pygame.init() # 初始化
pygame.init() # 初始化
class Block(pygame.sprite.Sprite): # 障碍物精灵类
def __init__(self,image1,image2,image3): class Block(pygame.sprite.Sprite): # 障碍物精灵类
super().__init__() def __init__(self, image1, image2, image3):
# 加载障碍物的图片 super().__init__()
self.image = random.choice([image1, image2, image3]) # 加载障碍物的图片
# 根据障碍物位图的宽高设置矩形 self.image = random.choice([image1, image2, image3])
self.rect = self.image.get_rect() # 根据障碍物位图的宽高设置矩形
# 障碍物绘制坐标 self.rect = self.image.get_rect()
self.rect.x = 1000 # 障碍物绘制坐标
self.rect.y = 500 - self.rect.height self.rect.x = 1000
self.rect.y = 500 - self.rect.height
class Player(pygame.sprite.Sprite): # 悟空 self.score = 1
def __init__(self, image):
super().__init__() class Player(pygame.sprite.Sprite): # 悟空
# 加载悟空精灵图像 def __init__(self, image):
self.image = image super().__init__()
# image的get_rect()方法,可以返回pygame.Rect(0,0,图像宽,图像高)的对象 # 加载悟空精灵图像
self.rect = self.image.get_rect() self.image = image
self.rect.x = 150 # image的get_rect()方法,可以返回pygame.Rect(0,0,图像宽,图像高)的对象
self.rect.y = 400 self.rect = self.image.get_rect()
self.rect.x = 150
# 创建一个窗口 self.rect.y = 400
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) # 创建一个窗口
pygame.display.set_caption("悟空酷跑") screen = pygame.display.set_mode((1000, 600))
# 载入图片 FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
background = pygame.image.load('bg.png') # 背景 pygame.display.set_caption("悟空酷跑")
road = pygame.image.load('road.png') # 路 # 载入图片
stone = pygame.image.load('stone.png') # 石头 background = pygame.image.load('bg.png') # 背景
cacti = pygame.image.load('cacti.png') # 仙人掌 road = pygame.image.load('road.png') # 路
bush = pygame.image.load('bush.png') # 灌木丛 stone = pygame.image.load('stone.png') # 石头
hero = [pygame.image.load('hero1.png'), cacti = pygame.image.load('cacti.png') # 仙人掌
pygame.image.load('hero2.png'), bush = pygame.image.load('bush.png') # 灌木丛
pygame.image.load('hero3.png'), hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero4.png'), pygame.image.load('hero2.png'),
pygame.image.load('hero5.png')] pygame.image.load('hero3.png'),
basic_font = pygame.font.Font('STKAITI.TTF',32) pygame.image.load('hero4.png'),
index = 0 pygame.image.load('hero5.png')]
y = 400 #score_audio = pygame.mixer.Sound('score.wav')
jumpState = "runing" basic_font = pygame.font.Font('STKAITI.TTF', 18) # 字体
t = 30
road_x = 0 index = 0
bg_x = 0 y = 400
time = 0 jumpState = "runing"
gamestate = True t = 30
road_x = 0
block_list =pygame.sprite.Group() # 创建精灵组 bg_x = 0
time = 0
while True: gamestate = True
for event in pygame.event.get(): score = 0
if event.type == locals.QUIT: old_score = score
# 接收到退出事件后退出程序
exit() block_list = pygame.sprite.Group() # 创建精灵组
if event.type == locals.KEYDOWN:
if jumpState == "runing": with open('record.txt', 'r', encoding='utf-8') as f:
if event.key == locals.K_SPACE: content = f.read() #文件读取操作
jumpState = "up" record = json.loads(content)
one = record["第1名"]
# 悟空造型 two = record["第2名"]
wukong = Player(hero[index]) three = record["第3名"]
if jumpState == "runing": # 跑步状态下
index += 1 while True:
if index >= 5: for event in pygame.event.get():
index = 0 if event.type == locals.QUIT:
# 接收到退出事件后退出程序
if gamestate == True: exit()
if jumpState == "up": # 起跳状态 if event.type == locals.KEYDOWN:
if t > 0: if jumpState == "runing":
y -= t if event.key == locals.K_SPACE:
wukong.rect.y = y jumpState = "up"
t -= 2
else: speed = 8 + score//3
jumpState = "down" # 悟空造型
if jumpState == "down": # 降落状态 wukong = Player(hero[index])
if t <= 30: if jumpState == "runing": # 跑步状态下
y += t index += 1
wukong.rect.y = y if index >= 5:
t += 2 index = 0
else:
jumpState = "runing" if gamestate == True:
t =30 if jumpState == "up": # 起跳状态
if t > 0:
# 将背景图画上去 y -= t
bg_x -= 1 wukong.rect.y = y
if bg_x<=-1000: t -= 2
bg_x = 0 else:
screen.blit(background, (bg_x, 0)) # 远景 jumpState = "down"
if jumpState == "down": # 降落状态
road_x -= 8 if t <= 30:
if road_x<=-1000: y += t
road_x = 0 wukong.rect.y = y
screen.blit(road, (road_x, 500)) # 道路 t += 2
else:
screen.blit(wukong.image, (150, y)) # 悟空 jumpState = "runing"
t = 30
time += 1
if time >= 60: # 创建障碍物精灵 # 将背景图画上去
time = 0 bg_x -= 1
num = random.randint(0,50) if bg_x <= -1000:
if num > 20: bg_x = 0
obstacle = Block(bush,cacti,stone) screen.blit(background, (bg_x, 0)) # 远景
block_list.add(obstacle)
for sprite in block_list: # 遍历、展示障碍物精灵 road_x -= speed
sprite.rect.x -= 8 if road_x <= -1000:
screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y)) road_x = 0
if sprite.rect.x <= 0-sprite.rect.width: screen.blit(road, (road_x, 500)) # 道路
sprite.kill()
if pygame.sprite.collide_rect(wukong, sprite): # 精灵碰撞检测 screen.blit(wukong.image, (150, y)) # 悟空
gameover = pygame.image.load('gameover.png') # 游戏结束
screen.blit(gameover, (400, 200)) time += 1
gamestate = False if time >= 60: # 创建障碍物精灵
scoreSurf = basic_font.render("分数:"+str(score),True,(255,0,0)) time = 0
screen.blit(scoreSurf,(850,20)) num = random.randint(0, 50)
if num > 20:
# 刷新画面 obstacle = Block(bush, cacti, stone)
pygame.display.update() block_list.add(obstacle)
FPS.tick(60) for sprite in block_list: # 遍历、展示障碍物精灵
\ No newline at end of file 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): #精灵碰撞检测
gameover = pygame.image.load('gameover.png') # 游戏结束
screen.blit(gameover, (400, 200))
gamestate = False
if score>three:
if score>one:
record["第1名"] = score
record["第2名"] = one
record["第3名"] = two
elif score > two:
record["第2名"] = score
record["第3名"] = two
else:
record["第3名"] = score
record = json.dumps(record, ensure_ascii=False)
with open("record.txt","w", encoding='utf-8') as f:
f.write(record)
else:
if sprite.rect.x + sprite.rect.width < wukong.rect.x:
score += sprite.score
sprite.score = 0
if score > old_score:
score_audio.play()
old_score = score
# 分数
scoreSurf = basic_font.render("分数:"+str(score), True, (255, 255, 255))
screen.blit(scoreSurf, (880,20))
scoreSurf = basic_font.render("第1名:"+str(one), True, (255, 255, 255))
screen.blit(scoreSurf, (880,50))
scoreSurf = basic_font.render("第2名:"+str(two), True, (255, 255, 255))
screen.blit(scoreSurf, (880,80))
scoreSurf = basic_font.render("第3名:"+str(three), True, (255, 255, 255))
screen.blit(scoreSurf, (880,110))
# 刷新画面
pygame.display.update()
FPS.tick(60)
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