0

I was making a game in pygame and when i went to add different music for the menu vs play time i encountered an issue with the players position not being updated period, so i simplified to the code to test just the player and when i run it as

main file


 import pygame

pygame.init()
from game.player import playerOBJ
p = playerOBJ((0, 255, 0), (5, 5), 5, (30, 30))

WIDTH = 800
HEIGHT = 600

wn = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()

running = True
while running:
    p.move()
    p.render(wn)
    clock.tick(60)
    pygame.display.flip()

player class

import pygame
pygame.init()

class playerOBJ:
    def __init__(self, color, size, vel, pos):
        self.color = color
        self.width, self.height = size

        self.vel = vel
        self.diag = 3.5
        self.up = 1
        self.left = 1
        self.down = 1
        self.right = 1

        self.xpos, self.ypos = pos

        self.shape = (self.xpos, self.ypos, self.width, self.height)
        self.rect = pygame.Rect(self.xpos, self.ypos, self.width, self.height)
        self.dead = False

    def render(self, screen):
        pygame.draw.rect(screen, self.color, self.shape)
    
    def move(self):

        key = pygame.key.get_pressed()
        if key[pygame.K_w]:
            if key[pygame.K_w] and key[pygame.K_a]:
                self.xpos -= self.diag * self.left
                self.ypos -= self.diag * self.up
                

            elif key[pygame.K_w] and key[pygame.K_d]:
                self.xpos += self.diag * self.right
                self.ypos -= self.diag * self.up

            elif key[pygame.K_w]:
                self.ypos -= self.vel * self.up

            
        elif key[pygame.K_s]:
            if key[pygame.K_s] and key[pygame.K_a]:
                self.xpos -= self.diag * self.left
                self.ypos += self.diag *self.down
            
            elif key[pygame.K_s] and key[pygame.K_d]:
                self.xpos += self.diag * self.right
                self.ypos += self.diag * self.down     

            elif key[pygame.K_s]:
                self.ypos += self.vel * self.down
            
        elif key[pygame.K_a]:
            self.xpos -= self.vel * self.left
        
        elif key[pygame.K_d]:
            self.xpos += self.vel * self.right

        self.shape = (self.xpos, self.ypos, self.width, self.height)
        self.rect = pygame.Rect(self.xpos, self.ypos, self.width, self.height)

i copied a working moving class from the test version of this project

1 Answers1

0

Try adding a pygame.event.get() call to your main.py file, from:

import pygame

pygame.init()
from game.player import playerOBJ
p = playerOBJ((0, 255, 0), (5, 5), 5, (30, 30))

WIDTH = 800
HEIGHT = 600

wn = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()

running = True
while running:
    p.move()
    p.render(wn)
    clock.tick(60)
    pygame.display.flip()

to:


import pygame

pygame.init()
from game.player import playerOBJ
p = playerOBJ((0, 255, 0), (5, 5), 5, (30, 30))

WIDTH = 800
HEIGHT = 600

wn = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    p.move()
    p.render(wn)
    clock.tick(60)
    pygame.display.flip()

pygame.quit()

Actually, the extra for loop isn't necessary if you don't care about checking for events such as closing the pygame window; a simple pygame.event.get() call would be enough.

Ann Zen
  • 25,080
  • 7
  • 31
  • 51