Commit a69d474b by BellCodeEditor

save project

parent 7ea0e575
Showing with 61 additions and 44 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,51 +57,60 @@ while True: ...@@ -49,51 +57,60 @@ while True:
if event.key == locals.K_SPACE: if event.key == locals.K_SPACE:
jumpState = "up" jumpState = "up"
# 悟空造型
if jumpState == "up": # 起跳状态 wukong =Player( hero[index])
if t > 0:
y -= t
t -= 2
else:
jumpState = "down"
if jumpState == "down": # 降落状态
if t <= 30:
y += t
t += 2
else:
jumpState = "runing"
t =30
# 悟空造型
wukong = hero[index]
if jumpState == "runing": # 跑步状态下 if jumpState == "runing": # 跑步状态下
index += 1 index += 1
if index >= 5: if index >= 5:
index = 0 index = 0
# 将背景图画上去 if status==True:
screen.blit(background, (0, 0)) # 远处背景 if jumpState == "up": # 起跳状态
screen.blit(road, (0, 500)) # 路 if t > 0:
screen.blit(wukong, (150, y)) y -= t
if time>=60: wukong.rect.y=y
time=0 t -= 2
num = random.randint(0,50) else:
if num >20: jumpState = "down"
obstacle=Block(bush,carti,stone) if jumpState == "down": # 降落状态
block.list.add(obstacle) if t <= 30:
y += t
wukong.rect.y=y
t += 2
else:
jumpState = "runing"
t =30
# 将背景图画上去
screen.blit(background, (0, 0)) # 远处背景
screen.blit(road, (0, 500)) # 路
screen.blit(wukong.img, (150, y))
time+=1
if time>=60:
time=0
num = random.randint(0,50)
if num >20:
obstacle=Block(bush,cacti,stone)
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 rect.x <= 0-rect.width: # 障碍物消失 if pygame.sprite.collide_rect(wukong,sprite):
# # 创建障碍物对象 gameover = pygame.image.load('gameover.png')
# obstacle = random.choice([bush,stone,cacti]) screen.blit(gameover,(400,200))
# rect = obstacle.get_rect() status = False
# rect.x = 1000 # if rect.x <= 0-rect.width: # 障碍物消失
# rect.y = 500 - rect.height # # 创建障碍物对象
# rect.x -= 8 # obstacle = random.choice([bush,stone,cacti])
# screen.blit(obstacle, (rect.x, rect.y)) # rect = obstacle.get_rect()
# 刷新画面 # rect.x = 1000
pygame.display.update() # rect.y = 500 - rect.height
FPS.tick(60) # rect.x -= 8
# screen.blit(obstacle, (rect.x, rect.y))
\ No newline at end of file # 刷新画面
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