Commit 922cdf42 by BellCodeEditor

save project

parent db0446f0
Showing with 87 additions and 65 deletions
import pygame import pygame
from pygame import locals
import random import random
from pygame import locals
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
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("悟空酷跑")
# 载入图片 # 载入图片
background = pygame.image.load('bg.png') # 背景 background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路 road = pygame.image.load('road.png') # 路
...@@ -25,62 +17,92 @@ hero = [pygame.image.load('hero1.png'), ...@@ -25,62 +17,92 @@ hero = [pygame.image.load('hero1.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')]
lu_x=0 block_list=pygame.sprite.Group()
shan_x=0 time=0
index = 0 class Block(pygame.sprite.Sprite):
y = 400 def __init__(self,image1,image2,image3):
jumpState = "runing" super().__init__()
t = 30 self.image=random.choice([image1,image2,image3])
aa=Block(bush,cacti,stone) self.rect=self.image.get_rect()
self.rect.x=1000
self.rect.y=0-self.rect.height
zhangai=Block(bush,cacti,stone)
game=1
t=30
index = 0
road_x=0
back_x=0
jumpstate='running'
y=400
pygame.display.set_caption('悟空跑酷')
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
while True: while True:
for event in pygame.event.get(): if game==1:
if event.type == locals.QUIT: for event in pygame.event.get():
# 接收到退出事件后退出程序 if event.type == locals.QUIT:
exit() # 接收到退出事件后退出程序
if event.type == locals.KEYDOWN: exit()
if jumpState == "runing": if event.type==locals.KEYDOWN:
if event.key == locals.K_SPACE: if jumpstate=='running':
jumpState = "up" if event.key==locals.K_SPACE:
jumpstate='up'
if jumpState == "up": # 起跳状态 if jumpstate=='up':
if t > 0: if t>0:
y -= t y-=t
t -= 2 t-=2
else: else:
jumpState = "down" jumpstate='down'
if jumpState == "down": # 降落状态 t=0
if t <= 30: if jumpstate=='down':
y += t if t<=30:
t += 2 y+=t
else: t+=2
jumpState = "runing" else:
t =30 jumpstate='running'
t=30
# 悟空造型
wukong = hero[index]
if jumpState == "runing": # 跑步状态下
index += 1
if index >= 5:
index = 0
# 将背景图画上去
shan_x -=2
if shan_x <-1000:
shan_x=0
screen.blit(background, (shan_x, 0)) # 远处背景
lu_x -=8
if lu_x <-1000:
lu_x=0
screen.blit(road, (lu_x, 500)) # 路
screen.blit(wukong, (150, y)) # 悟空
if aa.rect.x <= 0-aa.rect.width: # 障碍物消失
# 创建障碍物对象
aa=Block(bush,cacti,stone)
aa.rect.x -= 8
# 将背景图画上去
screen.blit(aa.image, (aa.rect.x, aa.rect.y)) back_x-=1
# 刷新画面 if back_x<=-1000:
pygame.display.update() back_x=0
FPS.tick(60) screen.blit(background, (back_x, 0))
\ No newline at end of file
road_x-=12
if road_x<=-1000:
road_x=0
screen.blit(road, (road_x, 500))
if jumpstate=='running':
if index>len(hero)-1:
index=0
wukong=Player(hero[index])
screen.blit(wukong.image, (150, y))
index+=1
time+=1
if time>=60:
time=0
num=random.randint(0,50)
if num>20:
zhangai=Block(bush,cacti,stone )
block_list.add(zhangai)
for sprite in block_list:
sprite.rect.x-=12
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))
game=0
# 刷新画面
pygame.display.update()
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