Commit 22f5efe7 by BellCodeEditor

save project

parent 5a0c6e2e
Showing with 21 additions and 14 deletions
...@@ -2,6 +2,13 @@ import pygame ...@@ -2,6 +2,13 @@ import pygame
from pygame import locals from pygame import locals
import random import random
pygame.init() # 初始化 pygame.init() # 初始化
class Books(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))
pygame.display.set_caption("悟空跑酷") pygame.display.set_caption("悟空跑酷")
...@@ -17,18 +24,16 @@ hero = [pygame.image.load('hero1.png'), ...@@ -17,18 +24,16 @@ hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero3.png'), pygame.image.load('hero3.png'),
pygame.image.load('hero4.png'), pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')] pygame.image.load('hero5.png')]
class Books(pygame.sprite.sprite):
index = 0 index = 0
y = 400 y = 400
t = 35 t = 35
road_x = 0 road_x = 0
beijing_x = 0 beijing_x = 0
jump = "running" jump = "running"
rect = cacti.get_rect() #rect = cacti.get_rect()
rect.x = 1000 #rect.x = 1000
rect.y = 455 - rect.width #rect.y = 455 - rect.width
obstacle=random.choice([stone,cacti,apple]) obstacle=Books(stone,cacti,apple)
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.QUIT: if event.type == locals.QUIT:
...@@ -52,29 +57,30 @@ while True: ...@@ -52,29 +57,30 @@ while True:
else: else:
jump = "running" jump = "running"
t = 35 t = 35
wukong = hero[index] wukong = hero[index]
if jump == "running": if jump == "running":
index += 1 index += 1
if index == 5: if index == 5:
index = 0 index = 0
# 将背景图画上去 # 将背景图画上去
beijing_x -= 4 beijing_x -= 4
if beijing_x <= -1000: if beijing_x <= -1000:
beijing_x = 0 beijing_x = 0
screen.blit(background, (beijing_x, 0)) screen.blit(background,(beijing_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)) screen.blit(wukong, (150, y))
if rect.x < 0-rect.width: #障碍物
obstacle=random.choice([stone,cacti,apple]) if obstacle.rect.x < 0-obstacle.rect.width:
rect = cacti.get_rect() obstacle = Books(stone,cacti,apple)
rect.x = 1000
rect.y = 455 - rect.width
else: else:
rect.x -= 8 obstacle.rect.x -= 8
screen.blit(obstacle,(rect.x, rect.y)) screen.blit(obstacle.image,(obstacle.rect.x, obstacle.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