Python Forum
[PyGame] pygame blit() method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] pygame blit() method
#1
why when I put the second argument of the blit() method with surf.get_rect() the image becomes in the upper left corner of the screen? When I change the integer value it remains at the top left corner of the screen ... but when I replace the argument with (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2) it changes place to almost in the middle of the screen

import pygame
from pygame.locals import (
    K_UP,
    K_DOWN,
    K_LEFT,
    K_RIGHT,
    K_ESCAPE,
    KEYDOWN,
    QUIT,
)

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600


class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.surf = pygame.Surface((75, 25))
        self.surf.fill((255, 255, 255))
        self.rect = self.surf.get_rect()


pygame.init()

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

player = Player()

running = True

while running:

    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
        elif event.type == QUIT:
            running = False

    screen.fill((0, 0, 0))

    screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
    
    pygame.display.flip()

pygame.quit()
Reply
#2
The blit function will draw your player surface at the coordinates you give it. So if you put (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2) then it will draw it there.

By saying surf.get_rect() you are just creating a rectangle from the surface, but the surface hasn't moved, so it will stay at 0,0 unless you give it new coordinates.

If you are trying to move the rectangle with the arrow keys then you can move the rectangle instead. Try adding this function to your player class:

def update(self):
        #get key press and adjust rectangle
        key = pygame.key.get_pressed()
        if key[pygame.K_RIGHT]:
            self.rect.x += 1
        if key[pygame.K_LEFT]:
            self.rect.x -= 1
        if key[pygame.K_UP]:
            self.rect.y -= 1
        if key[pygame.K_DOWN]:
            self.rect.y += 1

        screen.blit(self.surf, self.rect)
and then replace your screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2)) in the game loop with player.update()


This isn't the cleanest way to do this, but it may help explain what is going on. You just take key presses and then adjust the position of the rectangle of the player instance. Then you blit your surface at those coordinates
syafiq14 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  index error when using lists to blit rotated images codefun45 7 3,574 Sep-03-2020, 11:11 PM
Last Post: codefun45
  [PyGame] I found a way to generate sprites without .blit. Is it effecient? xBlackHeartx 19 8,489 Dec-07-2019, 01:06 PM
Last Post: metulburr
  moving image with blit rwahdan 2 3,045 Jul-10-2019, 06:24 PM
Last Post: nilamo
  error with survace.blit joemcsk1 3 4,470 Aug-06-2018, 12:23 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

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