Posts: 3
Threads: 1
Joined: May 2018
I created my first sprite with Pygame and managed to make it move with keyboard keys. However, when it encounters the edges of the screen, it keeps moving beyond them, disappearing from the screen. I would like to make it stop when it touches the edges, how can I do it?
This is my code: https://imgur.com/8N3hUTg
Posts: 7
Threads: 4
Joined: May 2018
Hi
I don't know how to solve your problem, however, it would make it easier for someone to help you if you used " insert python code" .
you can read about it above you post " please make sure to use python tags when inserting code into the forum"
example
1 2 3 4 |
import numpy as np
data = np.zero(( 5 , 5 ))
....
|
When it is formatted like this it so much easier to work with. Just copy and paste. This way we can play around with your code and see if we can get it to work. However, I am lazy, so I don't want to manually write it into my editor :P
BR Mark
Posts: 3
Threads: 1
Joined: May 2018
May-12-2018, 12:49 PM
(This post was last modified: May-12-2018, 12:49 PM by mrmn.)
(May-12-2018, 10:56 AM)Mark3232 Wrote: Hi
I don't know how to solve your problem, however, it would make it easier for someone to help you if you used " insert python code" .
you can read about it above you post " please make sure to use python tags when inserting code into the forum"
example
1 2 3 4 |
import numpy as np
data = np.zero(( 5 , 5 ))
....
|
When it is formatted like this it so much easier to work with. Just copy and paste. This way we can play around with your code and see if we can get it to work. However, I am lazy, so I don't want to manually write it into my editor :P
BR Mark
Ok. Here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import pygame
from pygame import *
import sys
WIDTH = 600
HEIGHT = 480
FPS = 60
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):
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 )
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)
while True :
clock.tick(FPS)
for event in pygame.event.get():
if event. type = = pygame.QUIT:
pygame.quit()
sys.exit()
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:
player.rect.right + = 5
all_sprites.update()
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.flip()
|
Posts: 5,151
Threads: 396
Joined: Sep 2016
May-12-2018, 10:24 PM
(This post was last modified: May-12-2018, 10:24 PM by metulburr.)
the simple method would be to clamp it in place of the entire screen.
In your player class if you add an update method and in it
1 |
self .rect.clamp_ip(screen.get_rect())
|
where self.rect is the rect of the player and screen.get_rect() is the screen from display and returns a rect of the screen size. But obviously you would want to pre determine the screen size and set it to that, not do it every frame.
and example would be here of similar task
https://python-forum.io/Thread-PyGame-Ba...ing-part-3
The 5th code box from the top
Recommended Tutorials:
Posts: 21
Threads: 5
Joined: Oct 2016
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import pygame
from pygame import *
import sys
WIDTH = 600
HEIGHT = 480
FPS = 60
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):
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 )
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)
while True :
clock.tick(FPS)
for event in pygame.event.get():
if event. type = = pygame.QUIT:
pygame.quit()
sys.exit()
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:
if player.rect.x + player.imange.get_width() < WIDTH:
player.rect.right + = 5
all_sprites.update()
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.flip()
|
Posts: 3
Threads: 1
Joined: May 2018
(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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import pygame
from pygame import *
import sys
WIDTH = 600
HEIGHT = 480
FPS = 60
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):
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 )
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)
while True :
clock.tick(FPS)
for event in pygame.event.get():
if event. type = = pygame.QUIT:
pygame.quit()
sys.exit()
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:
if player.rect.x + player.imange.get_width() < WIDTH:
player.rect.right + = 5
all_sprites.update()
screen.fill(BLACK)
all_sprites.draw(screen)
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
|