John Tobey
John Tobey

Reputation: 23

White Box in Pygame

Pretty Self-explanatory. My sprite does not show up and there is just a white box. I am using Ubuntu 18.04.1 LTS and pygame version 1.9.3 I am using the code from Programming the raspberry pi by Simon Monk on page 107 if you are wondering

import pygame
from pygame.locals import *
from sys import exit

spoon_x = 300
spoon_y = 300

pygame.init()

screen = pygame.display.set_mode((600,400))
pygame.display.set_caption('Raspberry Catching')

spoon = pygame.image.load('/home/john/PycharmProjects/pygame/spoon.png').convert()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
                exit()

    screen.fill((255,255,255))
    spoon_x, ignore = pygame.mouse.get_pos()
    screen.blit(spoon, (spoon_x, spoon_y))
    pygame.display.update()

spoon test

Upvotes: 2

Views: 325

Answers (1)

skrx
skrx

Reputation: 20438

Your image is very large and contains a lot of white areas, so if you blit it at the y-coord 300, you will only see some of the top white area and the spoon will be somewhere below the screen. You can see the spoon if you change spoon_y to -300.

I suggest cropping (removing most of the white areas around the spoon) and scaling the image in a graphics editor.

You could also use pygame.Surface.subsurface to crop the surface in pygame:

spoon_cropped = spoon.subsurface((295, 357, 1208, 273))

Or create another surface and blit the first surface onto it:

spoon_cropped = pygame.Surface((1208, 273))
# The third argument is the area that comprises the spoon.
spoon_cropped.blit(spoon, (0, 0), (295, 357, 1208, 273))

Upvotes: 1

Related Questions