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()