class FlappyDino(): def __init__(self, drop_location_x, drop_location_y): self.drop_location_x = drop_location_x self.drop_location_y = drop_location_y self.IsJump = False self.jump_count = 3 self.jump_count_implemented = self.jump_count self.walk_count = 0 self.activation_time = 0 self.frames_per_sprite = 3 self.belongtobao = True self.sprites = SpriteSheet("PyGame Pictures\\dino3.png", columns=5, rows=1, colour_key=Color(0, 0, 0)) self.sprites_duck = SpriteSheet("PyGame Pictures\\dino-duck3.png", columns=2, rows=1, colour_key=Color(0, 0, 0)) def fall(self): self.drop_location_y -= (self.jump_count_implemented * 10) self.jump_count_implemented -= 1 def jump(self): self.keys = pygame.key.get_pressed() if self.keys[pygame.K_UP]: self.IsJump = True self.activation_time += 1 if self.IsJump == True: if self.drop_location_y - self.jump_count_implemented * 10 >= 0: self.fall() if self.drop_location_y - self.jump_count_implemented * 10 < 0: self.jump_count_implemented = 0 self.fall() if self.keys[pygame.K_DOWN]: self.jump_count_implemented = -5 self.fall() if self.keys[pygame.K_UP] and self.activation_time % 2 == 0: self.jump_count_implemented = self.jump_count if self.drop_location_y > screen_height - y_axis_offset - dino_pixel_height: #71 is the dino's pixel height self.IsJump = False self.jump_count_implemented = self.jump_count if self.drop_location_y >= screen_height - y_axis_offset - dino_pixel_height: self.drop_location_y = screen_height - y_axis_offset - dino_pixel_height def DrawCharacter(self): global walk_count screen.blit(background_scaled, (backgroundX, 0)) screen.blit(background_scaled, (backgroundX2, 0)) self.hit_box_walking = (self.drop_location_x + 15, self.drop_location_y + 10, 30, dino_pixel_height - 10) self.hit_box_ducking = (self.drop_location_x + 10, self.drop_location_y + 30, 80, 35) if run == True: self.walk_count += 1 if self.drop_location_y >= screen_height - y_axis_offset - dino_pixel_height and self.keys[ pygame.K_DOWN]: if self.walk_count + 1 >= 2 * self.frames_per_sprite: self.walk_count = 0 self.sprites_duck.blit( screen, self.walk_count // self.frames_per_sprite, position=(self.drop_location_x, self.drop_location_y), origin=Origin.TopLeft) self.dino_hit_box = self.hit_box_ducking # pygame.draw.rect(screen, (255, 0, 0),self.dino_hit_box, 2) else: if self.walk_count + 1 >= 5 * self.frames_per_sprite: self.walk_count = 0 self.sprites.blit(screen, self.walk_count // self.frames_per_sprite, position=(self.drop_location_x, self.drop_location_y), origin=Origin.TopLeft) self.dino_hit_box = self.hit_box_walking
import time canvas = pygame.display.set_mode((200, 200)) # Test animation sprites = SpriteSheet('images/runners/heroine_animated.png', columns=32, rows=8, colour_key=pygame.Color(127, 127, 127)) # sprites = SpriteSheet('images/runners/hero_animated.png', columns=32, rows=8, colour_key=pygame.Color(127, 127, 127)) while True: # Direction = 0: Move W # Direction = 1: Move NW # Direction = 2: Move N # etc for direction in range(8): # Running: 4 - 11. Stand (with wobble): 0 - 3. Fall to lying flat: 18 - 23. Fall to knees: 18 - 21 for i in range(18, 22): canvas.fill((255, 255, 255)) sprites.blit(canvas, direction * 32 + i, position=(50, 50), origin=Origin.TopLeft) pygame.display.flip() time.sleep(0.2) time.sleep(1) while True: pass