Reputation: 45
I'm new to programming and my character goes out of the screen when I run the code. How can I make it stay inside the screen? Also, I have this wall.png which will work as the wall. How do I put the wall as the limit of the screen so it doesn't go away from the screen?
code:
import os, sys
import pygame
from pygame.locals import *
from ayuda import *
pygame.init
class PyManMain:
def __init__(self, width=600,height=600):
pygame.init()
self.width = width
self.height = height
self.screen = pygame.display.set_mode((self.width
, self.height))
def MainLoop(self):
self.LoadSprites();
pygame.key.set_repeat(500, 30)
self.background = pygame.Surface(self.screen.get_size())
self.background = self.background.convert()
self.background.fill((50,85,40))
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == KEYDOWN:
if ((event.key == K_RIGHT)
or (event.key == K_LEFT)
or (event.key == K_UP)
or (event.key == K_DOWN)):
self.snake.move(event.key)
lstCols = pygame.sprite.spritecollide(self.snake
, self.pellet_sprites
, True)
self.snake.pellets = self.snake.pellets + len(lstCols)
self.screen.blit(self.background, (0, 0))
self.pellet_sprites.draw(self.screen)
self.snake_sprites.draw(self.screen)
pygame.display.flip()
def LoadSprites(self):
self.snake = Snake()
self.snake_sprites = pygame.sprite.RenderPlain((self.snake))
nNumHorizontal = int(self.width/64)
nNumVertical = int(self.height/64)
self.pellet_sprites = pygame.sprite.Group()
for x in range(nNumHorizontal):
for y in range(nNumVertical):
self.pellet_sprites.add(Pellet(pygame.Rect(x*64, y*64, 64, 64)))
class Snake(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('personaje.png',-1)
self.pellets = 0
self.x_dist = 5
self.y_dist = 5
def move(self, key):
xMove = 0;
yMove = 0;
if (key == K_RIGHT):
xMove = self.x_dist
elif (key == K_LEFT):
xMove = -self.x_dist
elif (key == K_UP):
yMove = -self.y_dist
elif (key == K_DOWN):
yMove = self.y_dist
self.rect.move_ip(xMove,yMove);
class Pellet(pygame.sprite.Sprite):
def __init__(self, rect=None):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('cofrefront.png',-1)
if rect != None:
self.rect = rect
if __name__ == "__main__":
MainWindow = PyManMain()
MainWindow.MainLoop()
Upvotes: 0
Views: 247
Reputation: 996
I think I know what your problem is. You would like your character (snake) to stay inside the screen when he reaches the edges of the window. To achieve this, you need to modify the move
function of the Snake class :). My modifications would look like this:
def move(self, key):
xMove = 0;
yMove = 0;
if (key == K_RIGHT) and self.rect.right + self.x_dist <= 600 :
xMove = self.x_dist
elif (key == K_LEFT) and self.rect.left - self.x_dist >= 0:
xMove = -self.x_dist
elif (key == K_UP) and self.rect.top - self.y_dist >= 0:
yMove = -self.y_dist
elif (key == K_DOWN) and self.rect.bottom + self.y_dist <= 600:
yMove = self.y_dist
self.rect.move_ip(xMove,yMove);
Essentially what I did above, is before I let the character move left,right,up and down, I check whether he would be outside the visible screen. If after the movement he still is inside the screen, I let him move. If not, I block he's movement :)
Hope that helped,
Cheers!
Alex
Upvotes: 4