Python Forum
[PyGame] Changing from blocks to pictures
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Changing from blocks to pictures
#1
Dear fellow Python coders,

I have created a platformer where all of the graphics are blocky, but they are just pixels I coded. For example, for the platforms that the player stands on, I did this:
platforms = [
[20,180,30,10],
[20,140,30,10],
[20,100,30,10],
] 
PLATFORM = (128,128,128)#Grey
for platform in levels.platforms:
     pygame.draw.rect(window, PLATFORM, convertGridToPixels(platform, offset))
Everything works somewhat smoothly, but I want to change the graphics from blocks to images. How would I be able to do this? I want to mainly do this with the character itself, which is coded as such:
PLAYER = (255,255,255)#White
player = Rect([20,200,10,20])
scale = 2
offset = [0,25*scale]
centering = [255,300]
#Re-centering the screen around the player
offset[0] = -centering[0] + player.x*scale
offset[1] = -centering[1] + player.y*scale
#Draw player
draw = convertGridToPixels(player, offset)
pygame.draw.rect(window, PLAYER, draw)
 
Could you please help me change this format so I can insert a picture for the player instead of having it as a block?
Thank you for all your help!
Reply
#2
The basic format would be along the lines of such a class as the following. As yuu have it now you are just drawing a rect. Load an image and blit it to the players location at its assigned rect. Download this repo and playtest for examples. Check out the moving platforms one for side scroller platforms example.

class Player:
    def __init__(self, screen_rect):
        self.image = pygame.image.load('image.png').convert()
        self.rect = self.image.get_rect(center=screen_rect.center)
          
    def get_event(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.rect.x -= self.speed
            elif event.key == pygame.K_RIGHT:
                self.rect.x += self.speed
           
    def draw(self, surf):
        surf.blit(self.image, self.rect)
Recommended Tutorials:
Reply
#3
(Jan-12-2020, 11:17 PM)metulburr Wrote: The basic format would be along the lines of such a class as the following. As yuu have it now you are just drawing a rect. Load an image and blit it to the players location at its assigned rect. Download this repo and playtest for examples. Check out the moving platforms one for side scroller platforms example.

class Player:
    def __init__(self, screen_rect):
        self.image = pygame.image.load('image.png').convert()
        self.rect = self.image.get_rect(center=screen_rect.center)
          
    def get_event(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.rect.x -= self.speed
            elif event.key == pygame.K_RIGHT:
                self.rect.x += self.speed
           
    def draw(self, surf):
        surf.blit(self.image, self.rect)

So when I tried to implement this to my code, it says that
Quote:Unresolved reference 'screen_rect'
How do I fix this?
Reply
#4
screen_rect is assuming you have passed the pygame rect of the screen to the player class
screen = pygame.display.set_mode((600,600))
screen_rect = screen.get_rect()
...
player = Player(screen_rect)
You could just remove the argument to player class and put the player anywhere in the beginning. That is solely for placing the player in the center of the screen initially.
class Player:
    def __init__(self):
        self.image = pygame.image.load('image.png').convert()
        self.rect = self.image.get_rect(center=(100,100))
           
    def get_event(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.rect.x -= self.speed
            elif event.key == pygame.K_RIGHT:
                self.rect.x += self.speed
            
    def draw(self, surf):
        surf.blit(self.image, self.rect)
Recommended Tutorials:
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020