Commit 2389d947 by BellCodeEditor

save project

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