0

I am making a game through the pygame module and I wanted to make a skeleton walk left and right on the window but be stopped by the window "borders" (the left and right side of the screen). This should be 0 on the x coordinate (left) and 1800 on the right coordinate (right).

However, the 0 coordinate, when printed was not on the left border of the window but rather shifted right and the x coordinate on the left window border was approximately -125. I don't know why the 0 coordinate was shifted right and would like it to be returned to normal.

Here is the code:

import pygame
import os
pygame.init()

WIDTH, HEIGHT = 1800, 1000
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Animation in Act")

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

FPS = 60
VEL = 5

SKELETON_WIDTH, SKELETON_HEIGHT = 330, 330

direction_right = False
direction_left = False

isJump = False
jumpCount = 10

walkCount = 0


SKELETON_RIGHT_START = pygame.transform.scale(pygame.image.load(os.path.join("Assets", "pixil-frame-0.png")), (SKELETON_WIDTH, SKELETON_HEIGHT))
SKELETON_RIGHT_MIDDLE = pygame.transform.scale(pygame.image.load(os.path.join("Assets", "pixil-frame-1.png")), (SKELETON_WIDTH, SKELETON_HEIGHT))
SKELETON_RIGHT_END = pygame.transform.scale(pygame.image.load(os.path.join("Assets", "pixil-frame-2.png")), (SKELETON_WIDTH, SKELETON_HEIGHT))

SKELETON_LEFT_START = pygame.transform.flip(SKELETON_RIGHT_START, True, False)
SKELETON_LEFT_MIDDLE = pygame.transform.flip(SKELETON_RIGHT_MIDDLE, True, False)
SKELETON_LEFT_END = pygame.transform.flip(SKELETON_RIGHT_END, True, False)

walkRight = [SKELETON_RIGHT_START, SKELETON_RIGHT_MIDDLE, SKELETON_RIGHT_END]
walkLeft = [SKELETON_LEFT_START, SKELETON_LEFT_MIDDLE, SKELETON_LEFT_END]


def draw_window(skeleton):
    WIN.fill(WHITE)
    WIN.blit(SKELETON_RIGHT_START, (skeleton.x, skeleton.y))

    pygame.display.update()


def handle_skeleton_movement(keys_pressed, skeleton):
    global direction_right, direction_left, isJump, walkCount

    if keys_pressed[pygame.K_a] and skeleton.x - VEL + (skeleton.width // 3) > 0:
        skeleton.x -= VEL
        direction_left = True
        direction_right = False
    elif keys_pressed[pygame.K_d] and skeleton.x + VEL + 200 < WIDTH:
        skeleton.x += VEL
        direction_left = False
        direction_right = True
    elif skeleton.x < WIDTH // 2:
        direction_left = False
        direction_right = True
    elif skeleton.x > WIDTH // 2:
        direction_left = True
        direction_right = False

    if not isJump:
        if keys_pressed[pygame.K_SPACE]:
            isJump = True
            walkCount = 0


def main():
    skeleton = pygame.Rect(0, 600, SKELETON_WIDTH, SKELETON_HEIGHT)

    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()

        keys_pressed = pygame.key.get_pressed()
        handle_skeleton_movement(keys_pressed, skeleton)
        print(skeleton.x, skeleton.y)

        draw_window(skeleton)
main()

if __name__ == "__main__":
    main()

enter image description here

Mopps
  • 5
  • 2
  • @Rabbid76 Because when the skeleton is spawned at the 0 coordinate I could still fit about 2 skeleton widths between the blitted skeleton image and the left side of the screen. And since 0 is the edge of the window I cant move the skeleton left passed an invisible wall. Therefore the + (skeleton.width // 3) allows the skeleton to move past 0 into the negatives where the window screen edge is – Mopps Mar 15 '22 at 18:55
  • After moving the skeleton all the way left with the code above the output is ''' -105 600 ''' and I can still see the whole skeleton image – Mopps Mar 15 '22 at 18:57

1 Answers1

0

does the png of the skeleton have any blank space on either side of the skeleton?

remember the coordinates of your skeleton are the top-left corner of the png image you loaded and this is in the frame of your window where 0,0 is the top-left of your window

try using a solid coloured rectangle as your image and change your code to below and youll see that the rectangle goes right up to either side of your window

 if keys_pressed[pygame.K_a] and skeleton.x  > 0:
        skeleton.x -= VEL
        skeleton.x = max(0, skeleton.x);
        direction_left = True
        direction_right = False
    elif keys_pressed[pygame.K_d] and skeleton.x  + skeleton.width < WIDTH:
        skeleton.x += VEL
        skeleton.x = min(WIDTH-skeleton.width, skeleton.x);
        direction_left = False
        direction_right = True
coik
  • 1