Commit a69d474b by BellCodeEditor

save project

parent 7ea0e575
Showing with 31 additions and 14 deletions
...@@ -3,6 +3,7 @@ from pygame import locals ...@@ -3,6 +3,7 @@ from pygame import locals
import random import random
pygame.init() # 初始化 pygame.init() # 初始化
class Block(pygame.sprite.Sprite): class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3): def __init__(self,image1,image2,image3):
super().__init__() super().__init__()
...@@ -11,6 +12,14 @@ class Block(pygame.sprite.Sprite): ...@@ -11,6 +12,14 @@ class Block(pygame.sprite.Sprite):
self.rect.x = 1000 self.rect.x = 1000
self.rect.y = 500 - self.rect.height self.rect.y = 500 - self.rect.height
self.score = 1 self.score = 1
class Player(pygame.sprite.Sprite):
def __init__ (self,img):
super().__init__()
self.img=img
self.rect=self.img.get_rect()
self.rect.x=150
self.rect.y=400
#obstacle=Block(importpygame) #obstacle=Block(importpygame)
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((1000, 600)) screen = pygame.display.set_mode((1000, 600))
...@@ -35,9 +44,8 @@ t = 30 ...@@ -35,9 +44,8 @@ t = 30
# rect = obstacle.get_rect() # rect = obstacle.get_rect()
# rect.x = 1000 # rect.x = 1000
# rect.y = 500 - rect.height # rect.y = 500 - rect.height
obstacle = Block(bush,stone,cacti)
pygame
time = 0 time = 0
status=True
list=pygame.sprite.Group() list=pygame.sprite.Group()
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
...@@ -49,42 +57,51 @@ while True: ...@@ -49,42 +57,51 @@ while True:
if event.key == locals.K_SPACE: if event.key == locals.K_SPACE:
jumpState = "up" jumpState = "up"
# 悟空造型
wukong =Player( hero[index])
if jumpState == "runing": # 跑步状态下
index += 1
if index >= 5:
index = 0
if status==True:
if jumpState == "up": # 起跳状态 if jumpState == "up": # 起跳状态
if t > 0: if t > 0:
y -= t y -= t
wukong.rect.y=y
t -= 2 t -= 2
else: else:
jumpState = "down" jumpState = "down"
if jumpState == "down": # 降落状态 if jumpState == "down": # 降落状态
if t <= 30: if t <= 30:
y += t y += t
wukong.rect.y=y
t += 2 t += 2
else: else:
jumpState = "runing" jumpState = "runing"
t =30 t =30
# 悟空造型
wukong = hero[index]
if jumpState == "runing": # 跑步状态下
index += 1
if index >= 5:
index = 0
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) # 远处背景 screen.blit(background, (0, 0)) # 远处背景
screen.blit(road, (0, 500)) # 路 screen.blit(road, (0, 500)) # 路
screen.blit(wukong, (150, y)) screen.blit(wukong.img, (150, y))
time+=1
if time>=60: if time>=60:
time=0 time=0
num = random.randint(0,50) num = random.randint(0,50)
if num >20: if num >20:
obstacle=Block(bush,carti,stone) obstacle=Block(bush,cacti,stone)
block.list.add(obstacle) list.add(obstacle)
for sprite in list: for sprite in list:
sprite.rect.x-=8 sprite.rect.x-=8
screen.blit(sprite.image(sprite.rcet.x.sprite.rect.y)) screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y))
if sprite.rect.x<=0-sprite.rect.width: if sprite.rect.x<=0-sprite.rect.width:
sprite.skill() sprite.kill()
if pygame.sprite.collide_rect(wukong,sprite):
gameover = pygame.image.load('gameover.png')
screen.blit(gameover,(400,200))
status = False
# if rect.x <= 0-rect.width: # 障碍物消失 # if rect.x <= 0-rect.width: # 障碍物消失
# # 创建障碍物对象 # # 创建障碍物对象
# obstacle = random.choice([bush,stone,cacti]) # obstacle = random.choice([bush,stone,cacti])
......
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