这个游戏叫jumpy,大致玩法就是模拟超级玛丽一样,可以不停在各个档板上跳动,同时受到重力的作用,会向下掉,如果落下时,没有站在档板上,就挂了。
这节,我们加入重力因素,继续改造sprites.py
from part_03.settings import *
import pygame as pg
vec = pg.math.Vector2
class Player(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((30, 30))
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.center = WIDTH / 2, HEIGHT / 2
self.pos = self.rect.center
self.vel = vec(0, 0)
self.acc = vec(0, 0)
self.width = self.rect.width
self.height = self.rect.height
def update(self):
# 初始化时,垂直方向加入重力加速度
self.acc = vec(0, PLAYER_GRAVITY)
keys = pg.key.get_pressed()
if keys[pg.K_LEFT]:
self.acc.x = -PLAYER_ACC
if keys[pg.K_RIGHT]:
self.acc.x = PLAYER_ACC
self.acc.x += self.vel.x * PLAYER_FRICTION
self.vel += self.acc
self.pos += self.vel
if self.rect.left > WIDTH:
self.pos.x = 0 - self.width / 2
if self.rect.right < 0:
self.pos.x = WIDTH + self.width / 2
# 碰撞后,方块底部要停在档板上,所以要改成rect.midbottom
self.rect.midbottom = self.pos
# 档板类
class Platform(pg.sprite.Sprite):
def __init__(self, x, y, w, h):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((w, h))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
新建了一个Platform类,用来模拟档板,其实就是一个绿色的长矩形条;其次Player在update中,acc加速度初始化时,引入了垂直方向的加速度,其值仍然在settings.py中定义:
# game options
SIZE = WIDTH, HEIGHT = 360, 480
FPS = 60
DEBUG = True
TITLE = "Jumpy!"
# Player properties
PLAYER_ACC = 0.5
PLAYER_GRAVITY = 0.5 # 重力加速度
PLAYER_FRICTION = -0.06
# define color
BLACK = 0, 0, 0
WHITE = 255, 255, 255
RED = 255, 0, 0
GREEN = 0, 255, 0
BLUE = 0, 0, 255
YELLOW = 255, 255, 0
然后在main.py中使用这个Platform类:
from part_03.sprites import *
from part_03.settings import *
class Game:
def __init__(self):
pg.init()
pg.mixer.init()
self.screen = pg.display.set_mode(SIZE)
pg.display.set_caption(TITLE)
self.clock = pg.time.Clock()
self.running = True
self.playing = False
self.p1 = object
def new(self):
self.all_sprites = pg.sprite.Group()
# 创建一个档板Group
self.platforms = pg.sprite.Group()
self.player = Player()
self.all_sprites.add(self.player)
# 加入2个档板实例
self.p1 = Platform(0, HEIGHT - 40, WIDTH, 40)
p2 = Platform(WIDTH / 2 - 50, HEIGHT / 2 + 100, 100, 20)
self.all_sprites.add(self.p1)
self.all_sprites.add(p2)
self.platforms.add(self.p1)
self.platforms.add(p2)
g.run()
def run(self):
self.playing = True
while self.playing:
self.clock.tick(FPS)
self.events()
self.update()
self.draw()
def update(self):
self.all_sprites.update()
# 碰撞检测
hits = pg.sprite.spritecollide(self.player, self.platforms, False)
if hits:
self.player.pos.y = hits[0].rect.top
# 碰撞后,将player 垂直方向的速度归0(否则物体还是会继续向下掉)
self.player.vel.y = 0
def events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
if self.playing:
self.playing = False
self.running = False
def draw(self):
self.screen.fill(BLACK)
self.all_sprites.draw(self.screen)
self.debug()
pg.display.flip()
def debug(self):
if DEBUG:
font = pg.font.SysFont('Menlo', 25, True)
pos_txt = font.render(
'Pos:(' + str(round(self.player.pos.x, 2)) + "," + str(round(self.player.pos.y, 2)) + ")", 1, GREEN)
vel_txt = font.render(
'Vel:(' + str(round(self.player.vel.x, 2)) + "," + str(round(self.player.vel.y, 2)) + ")", 1, GREEN)
acc_txt = font.render(
'Acc:(' + str(round(self.player.acc.x, 2)) + "," + str(round(self.player.acc.y, 2)) + ")", 1, GREEN)
self.screen.blit(pos_txt, (20, 10))
self.screen.blit(vel_txt, (20, 40))
self.screen.blit(acc_txt, (20, 70))
pg.draw.line(self.screen, WHITE, (0, HEIGHT / 2), (WIDTH, HEIGHT / 2), 1)
pg.draw.line(self.screen, WHITE, (WIDTH / 2, 0), (WIDTH / 2, HEIGHT), 1)
def show_start_screen(self):
pass
def show_go_screen(self):
pass
g = Game()
g.show_start_screen()
while g.running:
g.new()
g.show_go_screen()
pg.quit()
这里使用到了spritecollide这个超级好用的方法,可以很轻松的搞定碰撞检测。
如果仔细观察的话,会发现一个小问题,方块掉到档板上后,一直在上下轻微晃动,从Vel的调试输出值,也能看到y方向的速度,一直在0.5和0之间切换。原因在于:Player的update()方法,初始化时,给了acc在y方向0.5的加速度(具体值在settings.py中通过PLAYER_GRAVITY定义), 这个0.5,直到碰撞后,在main.py中,才通过self.player.pos.y = hits[0].rect.top纠正回来,即代码先物体向下落0.5px, 然后再强制重新调整位置,让它向上拉0.5px.
改进方法:将sprites.py中Player的update()方法改成下面这样
def update(self):
# 初始化时,垂直方向加入重力加速度
self.acc = vec(0, PLAYER_GRAVITY)
keys = pg.key.get_pressed()
if keys[pg.K_LEFT]:
self.acc.x = -PLAYER_ACC
if keys[pg.K_RIGHT]:
self.acc.x = PLAYER_ACC
self.acc.x += self.vel.x * PLAYER_FRICTION
self.vel += self.acc
self.pos += self.vel
if self.rect.left > WIDTH:
self.pos.x = 0 - self.width / 2
if self.rect.right < 0:
self.pos.x = WIDTH + self.width / 2
# self.rect.midbottom = self.pos
# 校正0.5px上下抖动的问题
if abs(self.rect.bottom - self.pos.y) >= 1:
self.rect.bottom = self.pos.y
self.rect.x = self.pos.x - self.width / 2
即:最后三行,先判断下y轴方向的位置变化量,只有>=1px的情况下才更新,再运行下
已经没有刚才的抖动问题。注:个人感觉这更像是pygame在渲染机制上的一个缺陷,只有0.5px这种不足1px的位移,才会有这个问题,同学们可以尝试把PLAYER_GRAVITY从0.5改成2(即:让每次的y轴位移>1px),也不会有抖动问题。