奔跑与跳跃
本课属于 Coddy Pygame 学习旅程的“游戏世界”单元。第 29 课,共 36 课。
现在,探索者可以移动了,并实现了重力、跳跃和平台碰撞。每一帧,先沿 x 轴移动并推出墙壁,然后施加重力,沿 y 轴移动并着陆或撞到头部,接着检测是否掉出洞穴:
keys = pygame.key.get_pressed()
dx = 0
if keys[pygame.K_LEFT]:
dx -= SPEED
if keys[pygame.K_RIGHT]:
dx += SPEED
player.x += dx
for wall in walls:
if player.colliderect(wall):
if dx > 0:
player.right = wall.left
elif dx < 0:
player.left = wall.right
vel_y = min(vel_y + GRAVITY, MAX_FALL)
player.y += vel_y
on_ground = False
for wall in walls:
if player.colliderect(wall):
if vel_y > 0:
player.bottom = wall.top
on_ground = True
elif vel_y < 0:
player.top = wall.bottom
vel_y = 0
if player.top > HEIGHT:
player.midbottom = start
vel_y = 0跳跃逻辑放在事件循环中:当角色处于 on_ground 状态时,按下空格键或向上键会触发 KEYDOWN 事件,并设置 vel_y = JUMP;按键松开时触发 KEYUP 事件,会将快速上升的速度截断为 -4,让角色跳得较低。
在顶部添加速度常量:SPEED = 4、GRAVITY = 1、JUMP = -15、MAX_FALL = 12。使用这些数值,角色跳起约 100 像素:足以跳上两格高的平台,也足以越过两格宽的坑。
挑战
中等让探险者跑、跳和下落。
- 在顶部添加
SPEED = 4、GRAVITY = 1、JUMP = -15和MAX_FALL = 12,并在循环之前添加vel_y = 0和on_ground = False。 - 在事件处理中添加跳跃和短跳。
- 在事件处理之后,添加所示的每帧代码。
按“开始”,然后走进第一个坑里:你会重新开始。接着按“检查”。
亲手试试
import pygame
TILE = 36
WIDTH, HEIGHT = 576, 360
LEVEL = [
"................................................................",
"................................................................",
"................................................................",
"................................................................",
"#..............................................................#",
"#....................C.........................#####...........#",
"#......CC...C................CC.....CC.........#####.....CC....#",
"#....................#.........................D...............#",
"#.P.........#...E....#...^.......K......E......D.C....^......F.#",
"#######..####################..##########################..#####",
]
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Moss Caves")
clock = pygame.time.Clock()
def load(name):
return pygame.image.load("assets/images/" + name).convert_alpha()
background = pygame.image.load("assets/backgrounds/forest.png").convert_alpha()
grass_img = load("grass.png")
grass_left_img = load("grass_left.png")
grass_right_img = load("grass_right.png")
dirt_img = load("dirt.png")
player_img = load("player.png")
def solid(row, col):
if row < 0 or row >= len(LEVEL) or col < 0 or col >= len(LEVEL[0]):
return False
return LEVEL[row][col] == "#"
walls = []
tiles = []
for row, line in enumerate(LEVEL):
for col, char in enumerate(line):
rect = pygame.Rect(col * TILE, row * TILE, TILE, TILE)
if char == "#":
if solid(row - 1, col):
image = dirt_img
elif not solid(row, col - 1) and solid(row, col + 1):
image = grass_left_img
elif solid(row, col - 1) and not solid(row, col + 1):
image = grass_right_img
else:
image = grass_img
walls.append(rect)
tiles.append((image, rect))
elif char == "P":
start = rect.midbottom
player = player_img.get_rect(midbottom=start)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
for y in range(0, HEIGHT, 72):
for x in range(0, WIDTH, 72):
screen.blit(background, (x, y))
for image, rect in tiles:
screen.blit(image, rect)
screen.blit(player_img, player)
pygame.display.flip()
clock.tick(60)
pygame.quit()
游戏世界中的所有课
自己练习: Pygame 在线编辑器