Reputation: 67
I created a class of the player and I instantiated it and it doesn't work when I try to move it. I don't understand what is wrong. I thought the problem was in the order of the functions but isn't it.
Here is the code:
import pygame
from variabile import *
pygame.init()
screen = pygame.display.set_mode((1000,1000))
def ecranAlb():
WHITE = (255,255,255)
screen.fill(WHITE)
class Player():
def __init__(self,x,y):
self.x = x
self.y = y
def display(self):
rect = pygame.Rect(self.x,self.y,100,100)
pygame.draw.rect(screen,(255,255,0),rect)
def move(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
self.x = self.x + 2
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
a = Player(100,100)
ecranAlb()
a.move()
a.display()
pygame.display.flip()
Upvotes: 1
Views: 24
Reputation: 210909
You have to create the instance object of the Player
class before the application loop. When you do it in the loop, then the a new object at the initial position is generated in each frame:
a = Player(100,100) # <--- INSERET
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# a = Player(100,100) <--- DELETE
ecranAlb()
a.move()
a.display()
pygame.display.flip()
Upvotes: 2