Commit f9e74de8 by BellCodeEditor

save project

parent 815034ea
Showing with 90 additions and 34 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random
pygame.init() # 初始化 pygame.init() # 初始化
# 创建一个窗口 # 创建一个窗口
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'),pygame.image.load('hero2.png'),pygame.image.load('hero3.png'),pygame.image.load('hero4.png'),pygame.image.load('hero5.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')]
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
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
obstacle=Block(bush,stone,cacti)
index = 0 index = 0
t=30 r=0
y=400 y = 400
jumps="running" jumpState = "runing"
t = 30
pygamemode="ture"
#obstacle = random.choice([bush,cacti,stone])
b_list=pygame.sprite.Group()
road_x=0
back_x=0
time=0
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.K_SPACE: if jumpState == "runing":
if jumps=="running": if event.key == locals.K_SPACE:
jumps="down" jumpState = "up"
if jumps=="down": if pygamemode=="ture":
if t>0: wukong = Player(hero[index])
y-=t if jumpState == "runing": # 跑步状态下
t-=2 index += 1
else: if index >= 5:
jumps="up" index = 0
if jumps=="up": if jumpState == "up": # 起跳状态
if t<=30: if t > 0:
y+=t y -= t
t+=2 wukong.rect.y=y
t -= 2
else: else:
jumps="running" jumpState = "down"
t=30 if jumpState == "down": # 降落状态
ws=hero[index] if t <= 30:
if jumps=="running": y += t
index+=1 wukong.rect.y=y
t += 2
else: else:
index=0 jumpState = "runing"
if index >4: t =30
index=0 # 将背景图画上去 road_x-=8
screen.blit(background, (0, 0)) back_x-=1
screen.blit(road, (0, 500)) # 悟空造型
screen.blit(ws, (150, y)) if road_x<=-1000:
road_x=0
if back_x<=-1000:
back_x=0
# 将背景图画上去
screen.blit(background, (back_x, 0)) # 远处背景
screen.blit(road, (road_x, 500)) # 路
screen.blit(wukong.image,(150, y)) # 悟空
time+=1
if time>=50:
r=random.randint(0,100)
if r>40:
obstacle = Block(bush,cacti,stone)
b_list.add(obstacle)
time=0
for sprite in b_list:
sprite.rect.x-=8
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):
pygamemode="Fales"
gameover=pygame.image.load('gameover.png')
screen.blit(gameover,(400,500))
#if obstacle.rect.x <= 0-obstacle.rect.width: # 障碍物消失
# 创建障碍物对象
#obstacle = A(bush,cacti,stone)
#obztacle.rect.x -= 8
#screen.blit(obstacle.image, (obstacle.rect.x,obstacle.rect.y))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPS.tick(60) FPS.tick(100)
\ No newline at end of file \ 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