Spritesheet故障/代码审查

时间:2016-01-02 05:55:06

标签: python python-2.7 pygame

好的,所以我在这里得到了一些非常简单的代码,建议任何意见/建议。请记住,SIMPLE,换句话说,简洁明了。日Thnx。

我的问题是从png文件加载图像。因此,例如我在文件中有几个图像,并且我想在用户按下例如右箭头键时仅加载一行。基本上我分别为每个箭头键分别有4行,2行或3列 4行

#
import pygame, time, random
pygame.init()
HEIGHT = 700
WIDTH  = 1350
GRIDSIZE = HEIGHT/28
BLACK = (  0,  0,  0)
WHITE = (255,255,255)
RED   = (255,  0,  0)
screen=pygame.display.set_mode((WIDTH,HEIGHT))
font = pygame.font.SysFont("arial", 36)

shift = 10

#---------------------------------------#
#   classes                             #
#---------------------------------------#
class Sprite(pygame.sprite.Sprite):
    """ (fileName)
        Visible game object.
        Inherits Sprite to use its Rect property.
    """
    def __init__(self, picture=None):
        pygame.sprite.Sprite.__init__(self)
        self.x = 0
        self.y = 0
        self.visible = False
        self.image = self.blend_image(picture) 
        self.rect = self.image.get_rect()
        self.width, self.height = self.rect.width, self.rect.height
        self.update()

    def spawn(self, x, y):
        """ Assign coordinates to the object and make it visible.
        """
        self.x, self.y = x,y
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)       
        self.visible = True

    def draw(self, surface):
        surface.blit(self.image, self.rect)

    def update(self):             
    # after moving a sprite, the rect attribute must be updated
        self.rect = self.image.get_rect()
        self.rect = pygame.Rect(self.x,self.y,self.rect.width,self.rect.height)

    def moveLeft(self):
        self.x -= shift
        self.update()
    def moveRight(self):                   
        self.x += shift
        self.update()
    def moveUp(self):
        self.y -= shift
        self.update()
    def moveDown(self):
        self.y += shift
        self.update()

    def blend_image(self, file_name):
        """ Remove background colour of an image.
            Need to use it when a picture is stored in BMP or JPG format, with opaque background.
        """
        image_surface = pygame.image.load(file_name)
        image_surface = image_surface.convert() 
        colorkey = image_surface.get_at((0,0))  
        image_surface.set_colorkey(colorkey)    
        return image_surface

#---------------------------------------#
#   functions                           #
#---------------------------------------#
def redraw_screen():
    screen.fill(BLACK)
    world.draw(screen)
    if player.visible:
        player.draw(screen)
    pygame.display.update()

#---------------------------------------#
#   main program                        #
#---------------------------------------#    
world = Sprite("town.png")
world.spawn(0,0)
player = Sprite("player.png")
player.spawn(100,400)

LEFT_BORDER = 0
RIGHT_BORDER = WIDTH-1100
TOP_BORDER = 0
BOTTOM_BORDER = HEIGHT-480

#---------------------------------------#
clock = pygame.time.Clock()
FPS = 10
inPlay = True

while inPlay:               
    clock.tick(FPS)                      

# keyboard handler                                     
    pygame.event.get()                  
    keys = pygame.key.get_pressed()                                         
    if keys[pygame.K_ESCAPE]:           
        inPlay = False
# world moves opposite to the arrow      
    if keys[pygame.K_LEFT] and world.x < LEFT_BORDER:
        world.moveRight()
    if keys[pygame.K_RIGHT] and world.x > RIGHT_BORDER:
        world.moveLeft()
    if keys[pygame.K_UP] and world.y < TOP_BORDER:
        world.moveDown()
    if keys[pygame.K_DOWN] and world.y > -BOTTOM_BORDER:
        world.moveUp()

    redraw_screen()
#---------------------------------------#     
pygame.quit()

是的,所以这是使用python 2.7顺便说一句。 thnx再次感谢任何帮助。 而且,有一些东西被添加,没有用,但这是为了以后的代码,反之亦然。我可以做的一些调整,比如命名,稍后会出现。 如果玩家按下右键,我会看到一个面朝右边的男人的形象。 左,上,下相同的概念。 如果他拿着按钮,那个人就会跑出去&#34; 现在我有正常的位置和跑位。 png文件中有4行,2列,如何为各个键加载1行?

0 个答案:

没有答案