I am trying to make a simple platformer in pygame where the player will return to the begging of the window if they go to far to the right. However, my code is not working and the player is able to go off the screen and does not return to the start. This is my code so far:
class Player(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((30,40))
self.image.fill(green)
self.rect = self.image.get_rect()
self.rect.center = (width /2, height /2)
self.speed_x = 0
self.speed_y = 0
self.pos = 480
def update(self):
self.speed_x = 0
player_input = pg.key.get_pressed()
if player_input[pg.K_LEFT]:
self.speed_x = -5
if player_input[pg.K_RIGHT]:
self.speed_x = 5
self.rect.x += self.speed_x
self.rect.y += self.speed_y
# wrap around the sides of the screen
if self.pos > 480:
self.pos = 0
if self.pos < 0:
self.pos = -480,0