Commit eba81d69 by BellCodeEditor

save project

parent 465907a6
Showing with 23 additions and 22 deletions
...@@ -3,6 +3,14 @@ from pygame import locals ...@@ -3,6 +3,14 @@ from pygame import locals
import random import random
pygame.init() # 初始化 pygame.init() # 初始化
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
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((1000, 600)) screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
...@@ -21,15 +29,10 @@ index = 0 ...@@ -21,15 +29,10 @@ index = 0
zhuangtai = "running" zhuangtai = "running"
y = 400 y = 400
nu = 30 nu = 30
zhangai = random.choice([stone,cacti,bush])
#创建列表,障碍
rect = zhangai.get_rect()
#获取障碍x,y #获取障碍x,y
rect.x = 1000
rect.y = 500 - rect.height
road_x=0 road_x=0
bg_x = 0 bg_x = 0
zhuangzhe = Block(stone,cacti,bush)
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
...@@ -62,31 +65,28 @@ while True: ...@@ -62,31 +65,28 @@ while True:
wukong = hero[index] wukong = hero[index]
if zhuangtai == "running": if zhuangtai == "running":
index += 1 index += 1
if index == 5: if index == 5:
index = 0 index = 0
bg_x -= 2 bg_x -= 1
if bg_x <= -1000: if bg_x <= -1000:
bg_x = 0 bg_x = 0
screen.blit(background, (bg_x, 0)) screen.blit(background, (bg_x, 0))
road_x -= 8 road_x -= 8
if road_x <= -1000: if road_x <= -1000:
road_x = 0 road_x = 0
screen.blit(road, (road_x, 500)) screen.blit(road, (road_x, 500))
# 将背景图画上去 # 将背景图画上去
screen.blit(wukong, (150, y))
# 刷新画面
if zhuangzhe.rect.x <= 0-zhuangzhe.rect.width:
zhuangzhe = Block(stone,cacti,bush)
zhuangzhe.rect.x -= 8
screen.blit(wukong, (150, y)) screen.blit(zhuangzhe.image, (zhuangzhe.rect.x, zhuangzhe.rect.y))
# 刷新画面
if rect.x <= 0-rect.width:
zhangai = random.choice([stone,cacti,bush])
rect = zhangai.get_rect()
rect.x = 1000
rect.y = 500 - rect.height
rect.x -= 8
screen.blit(zhangai, (rect.x, rect.y))
pygame.display.update() pygame.display.update()
FPS.tick(25) 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