這個游戲叫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),也不會有抖動問題。