Commit a2dffbaa by BellCodeEditor

save project

parent fe699405
Showing with 64 additions and 20 deletions
...@@ -2,10 +2,27 @@ import pygame ...@@ -2,10 +2,27 @@ import pygame
import random import random
from pygame import locals 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() # 初始化 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.
# 载入图片 # 载入图片
background = pygame.image.load('bg.png') # 背景 background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路 road = pygame.image.load('road.png') # 路
...@@ -18,21 +35,25 @@ hero =[pygame.image.load('hero1.png'), ...@@ -18,21 +35,25 @@ hero =[pygame.image.load('hero1.png'),
pygame.image.load('hero4.png'), pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')] pygame.image.load('hero5.png')]
index = 0 index = 0
t=30 gamestat=True
road_x=0
time=0
background_x=0
t=35
jumpstat='running' jumpstat='running'
y=400 y=400
obstacle=random.choice([stone,cacti,apple]) obstacle=Block(stone,apple,cacti)
rect=obstacle.get_rect() Block_list=pygame.sprite.Group()
rect.x = 1000
rect.y = 500-rect.height
while True: while True:
wukong=Player(hero[index])
if jumpstat == 'running':
index+=1 index+=1
if index==5: if index==5:
index=0 index=0
wukong=hero[index]
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.KEYDOWN: if event.type == locals.KEYDOWN:
if event.key == locals.K_SPACE: if event.key == locals.K_SPACE:
...@@ -41,34 +62,56 @@ while True: ...@@ -41,34 +62,56 @@ while True:
if event.type == locals.QUIT: if event.type == locals.QUIT:
# 接收到退出事件后退出程序 # 接收到退出事件后退出程序
exit() exit()
if gamestat==True:
road_x-=8
if road_x<=-1000:#小路移动
road_x=0
background_x-=2
if background_x<=-1000:
background_x=0
if jumpstat=='jumpup': if jumpstat=='jumpup':
if t>0: if t>0:
y-=t y-=t
wukong.rect.y=y
t-=2 t-=2
else: else:
jumpstat='jumpdown' jumpstat='jumpdown'
if jumpstat=='jumpdown': if jumpstat=='jumpdown':
if t<=30: if t<=35:
y+=t y+=t
wukong.rect.y=y
t+=2 t+=2
else: else:
jumpstat='running' jumpstat='running'
t=30 t=35
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (background_x, 0))
screen.blit(road, (0, 500)) screen.blit(road, (road_x, 500))
screen.blit(wukong, (150, y)) 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
if rect.x<0-rect.width: screen.blit(obstacle.image,(obstacle.rect.x,obstacle.rect.y))
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() 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