Commit 2389d947 by BellCodeEditor

save project

parent 397f00e0
Showing with 22 additions and 12 deletions
import pygame import pygame
from pygame import locals 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):
...@@ -10,10 +9,17 @@ class Block(pygame.sprite.Sprite): ...@@ -10,10 +9,17 @@ class Block(pygame.sprite.Sprite):
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
self.rect.x = 1000 self.rect.x = 1000
self.rect.y = 500 - self.rect.height self.rect.y = 500 - self.rect.height
class Player(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
gamestate = True
screen = pygame.display.set_mode((1000, 600)) screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
pygame.display.set_caption("悟空酷跑") pygame.display.set_caption("悟空酷跑")
# 载入图片
background = pygame.image.load('bg.png') # 背景 background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路 road = pygame.image.load('road.png') # 路
stone = pygame.image.load('stone.png') # 石头 stone = pygame.image.load('stone.png') # 石头
...@@ -34,7 +40,8 @@ rect.x = 1000 ...@@ -34,7 +40,8 @@ rect.x = 1000
rect.y = 500 - rect.height rect.y = 500 - rect.height
bg_x = 0 bg_x = 0
road_x = 0 road_x = 0
obstacle = Block(bush,stone,cacti) time = 0
block_list = pygame.sprite.Group()
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:
...@@ -44,7 +51,6 @@ while True: ...@@ -44,7 +51,6 @@ while True:
if jumpState == "runing": if jumpState == "runing":
if event.key == locals.K_SPACE: if event.key == locals.K_SPACE:
jumpState = "up" jumpState = "up"
if jumpState == "up": # 起跳状态 if jumpState == "up": # 起跳状态
if t > 0: if t > 0:
y -= t y -= t
...@@ -58,8 +64,6 @@ while True: ...@@ -58,8 +64,6 @@ while True:
else: else:
jumpState = "runing" jumpState = "runing"
t =30 t =30
# 悟空造型
wukong = hero[index] wukong = hero[index]
if jumpState == "runing": # 跑步状态下 if jumpState == "runing": # 跑步状态下
index += 1 index += 1
...@@ -76,12 +80,17 @@ while True: ...@@ -76,12 +80,17 @@ while True:
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)) # 悟空
time += 1
if obstacle.rect.x <= 0-obstacle.rect.width: # 障碍物消失 if time >= 60:
# 创建障碍物对象 r = random.randint(0,100)
if r > 40:
obstacle = Block(bush,stone,cacti) obstacle = Block(bush,stone,cacti)
obstacle.rect.x -= 8 block_list.add(obstacle)
screen.blit(obstacle.image,(obstacle.rect.x,obstacle.rect.y)) time = 0
# 刷新画面 for sprite in block_list:
sprite.rect.x -= 8
screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y))
if sprite.rect.x<=0-sprite.rect.width:
sprite.kill()
pygame.display.update() pygame.display.update()
FPS.tick(30) FPS.tick(30)
\ 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