From bb73d5637b3cc8c16e92173e0fc5bb49ba569f59 Mon Sep 17 00:00:00 2001 From: dreac0nic Date: Wed, 19 Jan 2022 13:51:48 -0800 Subject: [PATCH 1/2] Apply tilt, roughly tied to gravity and flapping --- ple/games/flappybird/__init__.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ple/games/flappybird/__init__.py b/ple/games/flappybird/__init__.py index e24cfbd..dfc605c 100644 --- a/ple/games/flappybird/__init__.py +++ b/ple/games/flappybird/__init__.py @@ -30,9 +30,13 @@ def __init__(self, # all in terms of y self.vel = 0 + self.tilt = 0.0 self.FLAP_POWER = 9 * self.scale self.MAX_DROP_SPEED = 10.0 self.GRAVITY = 1.0 * self.scale + self.TILT_FLAP = 30.0 + self.TILT_FALLING = -70.0 + self.TILT_SPEED = 120.0 self.rng = rng @@ -50,6 +54,7 @@ def init(self, init_pos, color): self.game_tick = 0 self.pos_x = init_pos[0] self.pos_y = init_pos[1] + self.tilt = 0.0 def _oscillateStartPos(self): offset = 8 * np.sin(self.rng.rand() * np.pi) @@ -58,6 +63,7 @@ def _oscillateStartPos(self): def flap(self): if self.pos_y > -2.0 * self.image.get_height(): self.vel = 0.0 + self.tilt = self.TILT_FLAP self.flapped = True def update(self, dt): @@ -76,6 +82,7 @@ def update(self, dt): if self.vel < self.MAX_DROP_SPEED and self.thrust_time == 0.0: self.vel += self.GRAVITY + self.tilt = max(self.tilt - self.TILT_SPEED*dt, self.TILT_FALLING) # the whole point is to spread this out over the same time it takes in # 30fps. @@ -90,7 +97,8 @@ def update(self, dt): self.rect.center = (self.pos_x, self.pos_y) def draw(self, screen): - screen.blit(self.image, self.rect.center) + rotated_image = pygame.transform.rotate(self.image, self.tilt) + screen.blit(rotated_image, self.rect.center) class Pipe(pygame.sprite.Sprite): From 0ce34741fec8a8ad75d32cea96320c7490235bf1 Mon Sep 17 00:00:00 2001 From: dreac0nic Date: Wed, 19 Jan 2022 14:12:52 -0800 Subject: [PATCH 2/2] Draw the image from its rect This fixes the image being off-center, technically, as we are setting the position of its center, thus we should draw from the whole rect. --- ple/games/flappybird/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ple/games/flappybird/__init__.py b/ple/games/flappybird/__init__.py index dfc605c..96905ac 100644 --- a/ple/games/flappybird/__init__.py +++ b/ple/games/flappybird/__init__.py @@ -98,7 +98,7 @@ def update(self, dt): def draw(self, screen): rotated_image = pygame.transform.rotate(self.image, self.tilt) - screen.blit(rotated_image, self.rect.center) + screen.blit(rotated_image, self.rect) class Pipe(pygame.sprite.Sprite):