Daniel B
Daniel B

Reputation: 105

Pygame object on sprite not showing

I have a little problem when trying to learn about sprites in Pygame.

To begin with, i have already put a great amount of effort into this with sprites but i dont get it to work.

I have done exactly what guides on the internet are showing when drawing an object onto a sprite but object doesnt show up while running program.

Any suggestions would be more than welcome!

tick = 0
sprite = pygame.Surface([20, 20])
sprite.fill(white)
sprite.set_colorkey(white)
rect = sprite.get_rect()
rect.x = 400
rect.y = 300


while tick < 100:

    screen.fill(black)

    pygame.draw.rect(sprite, red, [rect.x, rect.y, 20, 20])
    pygame.display.update()
    clock.tick(10)
    tick += 1


pygame.quit()
quit()

Upvotes: 0

Views: 3923

Answers (2)

htiga
htiga

Reputation: 381

Pygame documents is a good friend.

1.Why would you fill sprite white and then set its color key to white? This will just make the sprite transparent. See set_colorkey.

sprite = pygame.Surface([20, 20])
sprite.fill(white)
sprite.set_colorkey(white)

If you need a red sprite, just create one and fill it red.

sprite = pygame.Surface((20, 20))
sprite.fill(red)

2.What pygame.draw.rect do is just drawing a rectangular shape on the Surface. So if you want to draw a red rect on the screen, just

pygame.draw.rect(screen, red, (0, 0, 20, 20))

Or if you want to show the sprite on the screen, which is usually more effective, use blit

screen.blit(sprite, (0, 0))

Have fun with pygame :)

Upvotes: 1

jsbueno
jsbueno

Reputation: 110208

So, there are two major problems with your code. First, you are filling the surface you are calling sprite with "white" and then just setting "white" to be treated as a transparent color for that surface, with the call to set_colorkey. So, nothing could show up anyway.

Second, you are drawing your rectangle on the Surface itself, not on your screen. So, you do suceed in painting it red, but you don't "stamp" it on the screen - you call pygame.draw.rect to draw on the Surface you are calling sprite itself, not on the screen.

Your code does not show you declaring a screen at all - I suppose you did that correctly, nonetheless keep in mind you always should post a complete example that will get the behavior you get there. If there is an error in your code to set up the screen, I can't know about it. Also, I've changed our call to draw.rect to call screen.blit : that will stamp whatever image is currently on the sprite on the surface owner of the blit method - in this case, the screen itself:

import pygame
screen = pygame.display.set_mode((800,600))
white = 255, 255, 255
tick = 0
sprite = pygame.Surface([20, 20])
sprite.fill(white)
sprite.set_colorkey(white)
rect = sprite.get_rect()
rect.x = 400
rect.y = 300


while tick < 100:

    screen.fill(black)
    screen.blit(sprite, (rect.x, rect.y))

    pygame.display.update()
    clock.tick(10)
    tick += 1

pygame.quit()
quit()

With that you should see a white rectangle shortly - from them on you can start improving your code to delay the program ending, or wait an user event to close the screen and get things rolling.

By the way, in Pygame talk, a Surface is not quite considered a "sprite" - a Sprite is a special class meant to be used as members of "groups" upon which a series of helper functions will work when you start organizing a more complete game or animated sequence.

Upvotes: 1

Related Questions