1

Alright, so I got some extremely simple code going on here, any comments/suggestions are recommended. Keep in mind, SIMPLE, in other words short and concise. thnx.

My problem is loading an image from a png file. So for example i got a couple of images in the file, and i want only one row to be loaded when the user presses for example, the right arrow key. Basically i have 4 rows, 2 or 3 columns 4 rows for each arrow key respectively

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

Right, so this is using python 2.7 btw. Any help is appreciated thnx again. And also, there are some things that are added that have no use, but thats for later code, and vice-versa. Some tweaks i could do, like naming, that will come later. If the player presses right, i have an image of a guy facing right. Left, up, and down same concept. if he holds the button, the guy "runs" Now i hav normal position, and running position. 4 rows, 2 columns in a png file, how to load 1 row for respective key?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Snake
  • 11
  • 3
  • you don't need `self.x`, `self.y`, `self.width`, `self.heigth`. Use always `self.rect.x`, `self.rect.y` and you will don't have to recreate `sel.rect`. – furas Jan 02 '16 at 07:42
  • easer and faster will be to load full file (at the beginning) and keep it in memory all time. Then you can use `subsurface()` to split it into single images. Don't waste time to load images during game. – furas Jan 02 '16 at 07:47
  • OK, I think i was recreating the self.rect because of the multiple images in the png file. It was so I could divide the file into halves, quarters etc. I don't really understand how to do that, could you explain it please?And yes it would be better to load the images at the start thnx, – Snake Jan 02 '16 at 16:52
  • you can use `pygame.Surface.subsurface` to create list of smaller images from one PNG file - like this `self.all_images[frame_number] = self.image.subsurface(pygame.Rect(offset_x, offset_y, width, height))` or `self.all_images[row][column] = self.image.subsurface(pygame.Rect(offset_x, offset_y, width, height))` – furas Jan 02 '16 at 17:22

0 Answers0