0
\$\begingroup\$

I'm working on a game where I want the player to appear on the other side of the screen when they get to the edge of the screen, essentially mimicking the background moving. When the player gets to the edge of the map, they shouldn't be able to move anymore. I'm just trying to get the first part to work where the player appears on the other side of the screen after hitting the edge. My full code is below.

#Import the pygame module
import pygame
     
from pygame.locals import (
    K_w,
    K_s,
    K_a,
    K_d,
    K_ESCAPE,
    KEYDOWN,
    QUIT,

)

BLUE = (0,0,255)
PURPLE = (153,50,204)
GREEN = (0,128,0)

MAP = [[BLUE, GREEN, BLUE],
       [GREEN, PURPLE, GREEN],
       [PURPLE, GREEN, BLUE]]


class Map():
    def __init__(self,map):
        self.map = map
    
    def update_map(self):
        pass

# Define constants for the screen width and height
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SPEED = 8
# Setup the clock for a decent framerate
clock = pygame.time.Clock()

# Define a player object by extending pygame.sprite.Sprite

# The surface drawn on the screen is now an attribute of 'player'
worldMaxX = SCREEN_WIDTH*3
worldMaxY = SCREEN_HEIGHT*3

cameraX = SCREEN_WIDTH
cameraY = SCREEN_HEIGHT



class Camera():
    def __init__(self):
        self.worldX = 0
        self.worldY = 0
        self.cameraX = SCREEN_WIDTH
        self.cameraY = SCREEN_HEIGHT
    def worldCoordinates(self,screenX, screenY):
        worldX = self.cameraX + screenX
        worldY = self.cameraY + screenY
        return (worldX, worldY)

    def screenCoordinates(self,worldX, worldY):
        screenX = worldX - cameraX
        screenY = worldY - cameraY
        return (screenX,screenY)

class Player(Camera):
    def __init__(self):
        super(Player,self).__init__()
        self.player_surface = pygame.Surface((25, 25))
        self.player_surface.fill((255, 0, 255))
        self.rect = self.player_surface.get_rect(center = (25,25))

        
    def update(self, pressed_keys):
        # Move the sprite based on user keypresses
        if pressed_keys[K_w]:
            self.worldX,self.worldY = self.worldCoordinates(self.rect.x,self.rect.y)
            self.worldY -= SPEED
            
            self.rect.x,self.rect.y =  self.screenCoordinates(self.worldX,self.worldY)

        if pressed_keys[K_s]:
            self.worldX,self.worldY = self.worldCoordinates(self.rect.x,self.rect.y)
            self.worldY += SPEED

            self.rect.x,self.rect.y =  self.screenCoordinates(self.worldX,self.worldY)

        if pressed_keys[K_a]:
            self.worldX,self.worldY = self.worldCoordinates(self.rect.x,self.rect.y)
            self.worldX -= SPEED

            self.rect.x,self.rect.y =  self.screenCoordinates(self.worldX,self.worldY)

        if pressed_keys[K_d]:
            self.worldX,self.worldY = self.worldCoordinates(self.rect.x,self.rect.y)
            self.worldX += SPEED
            
            self.rect.x,self.rect.y =  self.screenCoordinates(self.worldX,self.worldY)




    def updateCamera(self):
    #Move camera
        if (self.rect.x < 0):
            self.rect.x = SCREEN_WIDTH
        if (self.rect.x >= SCREEN_WIDTH):
            self.rect.x = 0
        
        if (self.rect.y <= 0):
            self.rect.y = SCREEN_HEIGHT
        
        if (self.rect.y >= SCREEN_HEIGHT):
            self.rect.y = 0

    def render(self,position):
        screen.blit(self.player_surface, position)



# Initialize pygame
pygame.init()


# Create the screen object
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))


# Instantiate player. Right now, this is just a rectangle.
player = Player()


# Variable to keep the main loop running
running = True


# Main loop
while running:

    # for loop through the event queue

    for event in pygame.event.get():
        # Check for KEYDOWN event

        if event.type == KEYDOWN:
            # If the Esc key is pressed, then exit the main loop
            if event.key == K_ESCAPE:
                running = False
        # Check for QUIT event. If QUIT, then set running to false.
        elif event.type == QUIT:
            running = False

    # Get all the keys currently pressed
    pressed_keys = pygame.key.get_pressed()

    # Fill the screen with black
    screen.fill((0, 0, 0))

    player.update(pressed_keys)
    player.updateCamera()
    # Draw the player on the screen
    player.render(player.rect)

    # Update the display
    pygame.display.flip()

    clock.tick(60)

What happens though is that my player square doesn't appear on the other side of the screen when going left or up. But, I don't understand why. I thought the code below would work, which it does when moving right, and down, but not up or left. What am I doing wrong with the camera method updateCamera that this isn't happening the way I expect it too?

    def updateCamera(self):
    #Move camera
        if (self.rect.x < 0):
            self.rect.x = SCREEN_WIDTH
        if (self.rect.x >= SCREEN_WIDTH):
            self.rect.x = 0
        
        if (self.rect.y <= 0):
            self.rect.y = SCREEN_HEIGHT
        
        if (self.rect.y >= SCREEN_HEIGHT):
            self.rect.y = 0
\$\endgroup\$
1
\$\begingroup\$

You need to use elif for your second conditional, not just if. If I role-play as your computer real quick with my self.rect.x value to something less than zero, this is what I say:

if (self.rect.x < 0):
            self.rect.x = SCREEN_WIDTH

Oh, OK! self.rect.x is less than zero, so I'll set it equal to SCREEN_WIDTH! Onward to the next line!

if (self.rect.x >= SCREEN_WIDTH):
            self.rect.x = 0

Oh, OK! self.rect.x is equal to SCREEN_WIDTH, so I'll set it right back to zero!

Hence, it works when going right and down (those are the second ifs), but not left or up. This is the code that should work:

if (self.rect.x < 0):
    self.rect.x = SCREEN_WIDTH
elif (self.rect.x >= SCREEN_WIDTH):
    self.rect.x = 0
\$\endgroup\$
2
  • \$\begingroup\$ Well that's a silly mistake to make. Thanks! Good catch! \$\endgroup\$ – justthom8 Jan 24 at 4:55
  • 1
    \$\begingroup\$ No problem! Happy developing! \$\endgroup\$ – Fluffy the Togekiss Jan 24 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.