Commit c252d48b by BellCodeEditor

auto save

parent dfb3ae82
Showing with 110 additions and 85 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random import random
class Block(pygame.sprite.Sprite): class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3): def __init__(self,image1,image2,image3):
super().__init__() 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.x=1000
self.rect.y=500-self.rect.height self.rect.y=500-self.rect.height
pygame.init() # 初始化 class Player(pygame.sprite.Sprite):
# 创建一个窗口 def __init__(self,image):
screen = pygame.display.set_mode((1000, 600)) super().__init__()
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) self.image=image
pygame.display.set_caption("悟空酷跑") self.rect=self.image.get_rect()
# 载入图片 self.rect.x=150
background = pygame.image.load('bg.png') # 背景 self.rect.y=400
road = pygame.image.load('road.png') # 路 pygame.init() # 初始化
stone = pygame.image.load('stone.png') # 石头 # 创建一个窗口
cacti = pygame.image.load('cacti.png') # 仙人掌 screen = pygame.display.set_mode((1000, 600))
bush = pygame.image.load('bush.png') # 灌木丛 FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
hero = [pygame.image.load('hero1.png'), pygame.display.set_caption("悟空酷跑")
pygame.image.load('hero2.png'), # 载入图片
pygame.image.load('hero3.png'), background = pygame.image.load('bg.png') # 背景
pygame.image.load('hero4.png'), road = pygame.image.load('road.png') # 路
pygame.image.load('hero5.png')] stone = pygame.image.load('stone.png') # 石头
road_x=0 cacti = pygame.image.load('cacti.png') # 仙人掌
bg_x=0 bush = pygame.image.load('bush.png') # 灌木丛
index = 0 hero = [pygame.image.load('hero1.png'),
y = 400 pygame.image.load('hero2.png'),
jumpState = "runing" pygame.image.load('hero3.png'),
t = 30 pygame.image.load('hero4.png'),
aa=Block(bush,cacti,stone) pygame.image.load('hero5.png')]
road_x=0
while True: bg_x=0
for event in pygame.event.get(): index = 0
if event.type == locals.QUIT: y = 400
# 接收到退出事件后退出程序 jumpState = "runing"
exit() t = 30
if event.type == locals.KEYDOWN: time=0
if jumpState == "runing": gamestate="true"
if event.key == locals.K_SPACE: block_list=pygame.sprite.Group()
jumpState = "up" while True:
for event in pygame.event.get():
if jumpState == "up": # 起跳状态 if event.type == locals.QUIT:
if t > 0: # 接收到退出事件后退出程序
y -= t exit()
t -= 2 if event.type == locals.KEYDOWN:
else: if jumpState == "runing":
jumpState = "down" if event.key == locals.K_SPACE:
if jumpState == "down": # 降落状态 jumpState = "up"
if t <= 30: wukong = Player(hero[index])
y += t if jumpState == "runing": # 跑步状态下
t += 2 index += 1
else: if index >= 5:
jumpState = "runing" index = 0
t =30 if gamestate=="true":
if jumpState == "up": # 起跳状态
# 悟空造型 if t > 0:
wukong = hero[index] y -= t
if jumpState == "runing": # 跑步状态下 wukong.rect.y=y
index += 1 t -= 2
if index >= 5: else:
index = 0 jumpState = "down"
# 将背景图画上去 if jumpState == "down": # 降落状态
bg_x -=2 if t <= 30:
if bg_x <-1000: y += t
bg_x=0 wukong.rect.y=y
screen.blit(background, (bg_x, 0)) # 远处背景 t += 2
road_x -=8 else:
if road_x <-1000: jumpState = "runing"
road_x=0 t =30
screen.blit(road, (road_x, 500)) # 路
screen.blit(wukong, (150, y)) # 悟空 # 悟空造型
if aa.rect.x <= 0-aa.rect.width: # 障碍物消失 # 将背景图画上去
# 创建障碍物对象 bg_x -=2
aa=Block(bush,cacti,stone) if bg_x <-1000:
aa.rect.x -= 8 bg_x=0
screen.blit(background, (bg_x, 0)) # 远处背景
road_x -=8
screen.blit(aa.image, (aa.rect.x, aa.rect.y)) if road_x <-1000:
# 刷新画面 road_x=0
pygame.display.update() screen.blit(road, (road_x, 500)) # 路
screen.blit(wukong.image, (150, y)) # 悟空
time+=1
if time>=60:
time=0
num=random.randint(0,50)
if num>10:
zaw=Block(bush,stone,cacti)
block_list.add(zaw)
for i in block_list:
i.rect.x-=8
screen.blit(i.image,(i.rect.x,i.rect.y))
if i.rect.x<=0-i.rect.width:
i.kill()
if pygame.sprite.collide_rect(wukong,i):
gameover = pygame.image.load('gameover.png')
screen.blit(gameover,(400,200))
gamestate="fales"
# 刷新画面
pygame.display.update()
FPS.tick(60) FPS.tick(60)
\ 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