Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Napkin Platformer
#4
1. There shouldn't be a list of the same image loaded several time
images = [
    pygame.image.load('napkin.png').convert_alpha(),
    pygame.image.load('napkin2.png').convert_alpha(),
    pygame.image.load('napkin3.png').convert_alpha()]


walk_left = 0, 2, 0, 2, 0, 2, 0, 2
walk_count = 0
# now you use position of walk_left to call loaded image
image = images[walk_left[walk_count]]
Here a full example.
background needs to be a bigger size for quality.
napkins needs transparent background.
import pygame
pygame.init()


# Did code to python pep 8 style guide.
# https://www.python.org/dev/peps/pep-0008/
class Player:
    @classmethod
    def load_images(cls):
        # convert image to pygame format will help with speed
        # convert_alpha() will not show invisable parts with pngs and jpg
        cls.images = [
            pygame.image.load('napkin.png').convert_alpha(),
            pygame.image.load('napkin2.png').convert_alpha(),
            pygame.image.load('napkin3.png').convert_alpha()]

    def __init__(self, x, y, w, h):
        self.rect = pygame.Rect(x, y, w, h)
        self.velocity = 5
        self.is_jumping = False
        self.jump_count = 5
        self.walk_left = 0, 2       # self.walk_left == (0, 2)
        self.walk_right = 0, 1
        self.walk_count = 0
        self.walk_pos = 0
        self.tick = 100
        self.next_tick = 100

    def can_move(self, ticks):
        if ticks > self.tick:
            self.tick += self.next_tick
            return True
        return False

    def draw(self, surface):
        surface.blit(Player.images[self.walk_pos], self.rect)

    def move_left(self):
        if self.rect.x > self.velocity:
            self.walk_count = (self.walk_count + 1) % len(self.walk_left)
            self.walk_pos = self.walk_left[self.walk_count]
            self.rect.x -= self.velocity

    def move_right(self, width):
        if self.rect.x < width - self.rect.width - self.velocity:
            self.walk_count = (self.walk_count + 1) % len(self.walk_right)
            self.walk_pos = self.walk_right[self.walk_count]
            self.rect.x += self.velocity

class Scene:
    def __init__(self):
        # basic pygame setup
        pygame.display.set_caption('Napkin Move')
        self.rect = pygame.Rect(0, 0, 1364, 500)
        self.surface = pygame.display.set_mode(self.rect.size)
        self.clock = pygame.time.Clock()

        # Scene setup
        Player.load_images()
        self.background = pygame.image.load('bg1.png').convert_alpha()
        self.background = pygame.transform.scale(self.background, self.rect.size)
        self.player = Player(300, 410, 64, 64)

    def mainloop(self):
        self.running = True
        while self.running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_a:
                        self.player.move_left()
                    elif event.key == pygame.K_d:
                        self.player.move_right(self.rect.width)

            ticks = pygame.time.get_ticks()
            keys = pygame.key.get_pressed()

            if self.player.can_move(ticks):
                if keys[pygame.K_LEFT]:
                    self.player.move_left()
                elif keys[pygame.K_RIGHT]:
                    self.player.move_right(self.rect.width)

            # drawing
            self.surface.blit(self.background, (0,0))
            self.player.draw(self.surface)

            # draw code here

            pygame.display.flip()
            self.clock.tick(30)

if __name__ == '__main__':
    scene = Scene()
    scene.mainloop()
    pygame.quit()
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
Napkin Platformer - by abscorpy - Jan-24-2019, 10:24 PM
RE: Napkin Platformer - by Windspar - Jan-25-2019, 12:12 AM
RE: Napkin Platformer - by abscorpy - Jan-26-2019, 10:52 AM
RE: Napkin Platformer - by Windspar - Jan-26-2019, 03:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] platformer enemy animation Yegor123 3 2,067 May-03-2023, 08:42 AM
Last Post: Windspar
  Tkinter platformer game Linch1 1 5,246 Mar-10-2019, 03:17 AM
Last Post: Windspar
  platformer problem abscorpy 1 2,446 Dec-11-2018, 11:08 PM
Last Post: Windspar
  platformer problem abscorpy 4 3,674 Nov-27-2018, 08:46 PM
Last Post: nilamo
  Help On Platformer Spectroxis 2 11,809 Apr-27-2017, 09:56 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020