我的角色并不留在屏幕内

时间:2014-03-22 22:38:42

标签: python-2.7 pygame

我是编程的新手,当我运行代码时,我的角色会离开屏幕。如何让它留在屏幕内?另外,我有这个wall.png可以作为墙。如何将墙壁作为屏幕的限制,以便它不会离开屏幕?

代码:

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()

1 个答案:

答案 0 :(得分:4)

我想我知道你的问题是什么。当你到达窗户边缘时,你希望你的角色(蛇)留在屏幕内。为此,您需要修改Snake类的move函数:)。我的修改看起来像这样:

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);

基本上我上面做的是,在我让角色向左,向右,向上和向下移动之前,我检查他是否会在可见屏幕之外。如果在运动之后他仍然在屏幕内,我让他移动。如果没有,我阻止他的运动:)

希望有所帮助,

干杯!

亚历