Python Forum
[PyGame] How to stop the sprite's movement when it touches the edges of the screen?
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] How to stop the sprite's movement when it touches the edges of the screen?
#6
(May-13-2018, 03:31 PM)Raures Wrote: I'd do it this way, check if player's sprite x axis position + player's sprite width is lower than screen width, if it is, then move sprite to the right.
Example in your code:

import pygame
from pygame import *
import sys
 
WIDTH = 600
HEIGHT = 480
FPS = 60
 
# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
 
class Player(pygame.sprite.Sprite):
    # sprite for the player
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 50))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH / 2, HEIGHT / 2)
 
# initialize pygame and create window
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My game")
clock = pygame.time.Clock()
pygame.key.set_repeat(True)
 
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
 
# Game loop
while True:
    # keep loop running at the right speed
    clock.tick(FPS)
    # Process input (events)
    for event in pygame.event.get():
        # check for closing window
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        # Moving sprites with arrow keys or WASD
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP or event.key == K_w:
                player.rect.centery -= 5
            elif event.key == pygame.K_DOWN or event.key == K_s:
                player.rect.centery += 5
            elif event.key == pygame.K_LEFT or event.key == K_a:
                player.rect.left -= 5
            elif event.key == pygame.K_RIGHT or event.key == K_d:
                # basically, if (sprite.x + sprite.width < screen.width): sprite.x += 5
                if player.rect.x + player.imange.get_width() < WIDTH:
                    # not sure if methods are correct
                    player.rect.right += 5
 
    # Update
    all_sprites.update()
 
    # Draw / render
    screen.fill(BLACK)
    all_sprites.draw(screen)
    # *after* drawing everything, flip the display
    pygame.display.flip()

I tried it, but this way the sprite only moves upward-right, stops only at the right edge and refuses to move leftward
Reply


Messages In This Thread
RE: How to stop the sprite's movement when it touches the edges of the screen? - by mrmn - May-13-2018, 06:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] When I hit the space bar from the home screen, it sends me to the game over screen JesusisKing 1 1,157 Apr-30-2023, 10:11 PM
Last Post: deanhystad
  Laggy Game Movement game_slayer_99 12 4,801 Oct-05-2022, 11:34 AM
Last Post: metulburr
  [PyGame] Particle movement mystery XavierPlatinum 10 3,272 Jul-09-2022, 04:28 AM
Last Post: deanhystad
  [PyGame] Isometric Movement on Tiled Map Josselin 0 2,485 Nov-02-2021, 06:56 AM
Last Post: Josselin
  [PyGame] object's movement to left leave shadow on the screen Zhaleh 3 3,299 Aug-02-2020, 09:59 PM
Last Post: nilamo
  [PyGame] Sprite image.get_rect() moves sprite to 0, 0 michael1789 2 4,720 Dec-13-2019, 08:37 PM
Last Post: michael1789
  Sprite not rendering Clunk_Head 2 2,285 Oct-03-2019, 11:27 AM
Last Post: Clunk_Head
  Using classes for room movement (text game) Lawr3y 3 6,721 Aug-20-2019, 12:40 AM
Last Post: Lawr3y
  Need help making a sprite GalaxyCoyote 4 3,392 Aug-11-2019, 09:12 PM
Last Post: metulburr
  Problem with coding for movement keys Aresofthesea 3 3,572 Jul-05-2019, 07:05 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020