Commit 01192a86 by BellCodeEditor

auto save

parent 080ee70a
Showing with 89 additions and 37 deletions
......@@ -3,10 +3,28 @@ from pygame import locals
import random
pygame.init() # 初始化
class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3): #__init__ 这里的杠杠是两个,不要忽视了
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
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
# 创建一个窗口
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') # 路
......@@ -18,14 +36,22 @@ hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero3.png'),
pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')]
index = 0
index = 0
y = 400
jumpState = "runing"
t = 30
obstacle = random.choice([bush, stone, cacti])
rect = obstacle.get_rect()
rect.x = 1000
rect.y = 500 - rect.height
#创建障碍物
obstacle = Block(bush, stone, cacti)
road_x = 0
bg_x = 0
time = 0
gamestate = True
block_list = pygame.sprite.Group() #精灵组
block_list.add(obstacle)
while True:
for event in pygame.event.get():
......@@ -37,39 +63,65 @@ while True:
if event.key == locals.K_SPACE:
jumpState = "up"
if jumpState == "up": # 起跳状态
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]
wukong = Player(hero[index])
if jumpState == "runing": # 跑步状态下
index += 1
if index >= 5:
index = 0
# 将背景图画上去
screen.blit(background, (0, 0)) # 远处背景
screen.blit(road, (0, 500)) # 路
screen.blit(wukong, (150, y)) # 悟空
if rect.x <= 0-rect.width: # 障碍物消失
# 创建障碍物对象
obstacle = random.choice([bush,stone,cacti])
rect = obstacle.get_rect()
rect.x = 1000
rect.y = 500 - rect.height
rect.x -= 8
screen.blit(obstacle, (rect.x, rect.y))
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
if gamestate == True:
if jumpState == "up": # 起跳状态
if t > 0:
y -= t
wukong.rect.y = y
t -= 2
else:
jumpState = "down"
if jumpState == "down": # 降落状态
if t <= 30:
y += t
wukong.rect.y = y
t += 2
else:
jumpState = "runing"
t =30
# 将背景图画上去
#------------------背景移动-----------------------------------
screen.blit(background, (bg_x, 0)) # 远处背景 (远景移动较慢)
bg_x -= 1
if bg_x <= -1000:
bg_x = 0
screen.blit(wukong.image, (150, y)) # 悟空 要注意画图的顺序,如果先加载悟空,那就会被背景图遮住
#-----------------道路移动--------------------------------------
road_x -= 8
screen.blit(road, (road_x, 500)) # 路
if road_x <= -1000: #此处代码含义:道路图片的长度是2000(可以在属性里看到),所以当图片的一半超出
road_x = 0 #左边缘,就需要重新设置坐标,不然继续移动的话画面就会出现断层,所以条件是road-x<=-1000
time += 1
if time >= 60:
r = random.randint(0,100)
if r > 50:
obstacle = Block(bush, stone, cacti)
block_list.add(obstacle)
time = 0 #如果time = 0放在if判断里面,那么当time为60时,就会随机一个r,如果不大于60,
#又会立马随机一个,所以就相当于是每当time=60,就会加一个精灵进列表,所以障碍物是等距离出现
#遍历障碍物并展示
for i in block_list:
i.rect.x -= 8
screen.blit(i.image, (i.rect.x, i.rect.y))
if i.rect.x <= 0-i.rect.width: # 障碍物消失
i.kill()
if pygame.sprite.collide_rect(wukong,obstacle): #精灵碰撞检测
gameover = pygame.image.load('gameover.png')
screen.blit(gameover,(400,200))
gamestate = False
# 刷新画面
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