Commit 34638ce0 by BellCodeEditor

auto save

parent cadf652d
Showing with 16 additions and 89 deletions
File added
......@@ -2,22 +2,7 @@ import pygame
from pygame import locals
import random
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
self.score = 1
class Wukong(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() # 初始化
# 创建一个窗口
......@@ -35,87 +20,29 @@ hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero3.png'),
pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')]
gameover = pygame.image.load('gameover.png')
lu_x=0
shan_x=0
index = 0
gamestate = True
score = 0
y = 400
jumpState = "runing"
t = 30
my_sprite = pygame.sprite.Group()
time = 0
shan_x = 0
lu_x = 0
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN:
if jumpState == "runing":
if event.key == locals.K_SPACE:
jumpState = "up"
if gamestate == True:
wukong = Wukong(hero[index])
if jumpState == "runing": # 跑步状态下
index += 1
if index >= 5:
index = 0
if jumpState == "up": # 起跳状态
if t > 0:
y -= t
wukong.rect.y = y
t -= 2
else:
jumpState = "down"
if jumpState == "down": # 降落状态
if t <= 30:
y += t
wukong.rect.y = y
t += 2
else:
jumpState = "runing"
t =30
# 悟空造型
# 将背景图画上去
shan_x -=2
if shan_x <-1000:
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.image, (150, y)) # 悟空
time += 1
if time >= 60:
time = 0
num = random.randint(0,50)
if num > 20:
obstacle=Block(bush,cacti,stone)
my_sprite.add(obstacle)
for sprite in my_sprite:
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):
screen.blit(gameover,(400,200))
gamestate = False
else:
screen.blit(background, (shan_x, 0))
screen.blit(road,(0,500))
# 刷新画面
pygame.display.update()
......
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