Commit a2dffbaa by BellCodeEditor

save project

parent fe699405
Showing with 82 additions and 39 deletions
......@@ -2,10 +2,27 @@ import pygame
import random
from pygame import locals
class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3):
super().__init__()
self.image=random.choice([stone,cacti,apple])
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
pygame.init() # 初始化
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
#pygame.display.set.
# 载入图片
background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路
......@@ -18,21 +35,25 @@ hero =[pygame.image.load('hero1.png'),
pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')]
index = 0
t=30
gamestat=True
road_x=0
time=0
background_x=0
t=35
jumpstat='running'
y=400
obstacle=random.choice([stone,cacti,apple])
rect=obstacle.get_rect()
rect.x = 1000
rect.y = 500-rect.height
obstacle=Block(stone,apple,cacti)
Block_list=pygame.sprite.Group()
while True:
index+=1
if index==5:
index=0
wukong=hero[index]
wukong=Player(hero[index])
if jumpstat == 'running':
index+=1
if index==5:
index=0
for event in pygame.event.get():
if event.type == locals.KEYDOWN:
if event.key == locals.K_SPACE:
......@@ -41,34 +62,56 @@ while True:
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if jumpstat=='jumpup':
if t>0:
y-=t
t-=2
else:
jumpstat='jumpdown'
if jumpstat=='jumpdown':
if t<=30:
y+=t
t+=2
else:
jumpstat='running'
t=30
if gamestat==True:
# 将背景图画上去
screen.blit(background, (0, 0))
screen.blit(road, (0, 500))
screen.blit(wukong, (150, y))
road_x-=8
if road_x<=-1000:#小路移动
road_x=0
if rect.x<0-rect.width:
obstacle=random.choice([stone,cacti,apple])
rect=obstacle.get_rect()
rect.x = 1000
rect.y = 500-rect.height
rect.x -= 8
screen.blit(obstacle,(rect.x,rect.y))
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
background_x-=2
if background_x<=-1000:
background_x=0
if jumpstat=='jumpup':
if t>0:
y-=t
wukong.rect.y=y
t-=2
else:
jumpstat='jumpdown'
if jumpstat=='jumpdown':
if t<=35:
y+=t
wukong.rect.y=y
t+=2
else:
jumpstat='running'
t=35
# 将背景图画上去
screen.blit(background, (background_x, 0))
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>20:
obstacle=Block(stone,apple,cacti)
Block_list.add(obstacle)
for sprite in Block_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):
gameover=pygame.image.load('gameover.png')
screen.blit(gameover,(400,150))
gamestat=False
screen.blit(obstacle.image,(obstacle.rect.x,obstacle.rect.y))
# 刷新画面
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