Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good way to draw images using pygame?

I would like to know how to draw images using pygame. I know how to load them. I have made a blank window. When I use screen.blit(blank, (10,10)), it does not draw the image and instead leaves the screen blank.

like image 507
jtl999 Avatar asked Jan 15 '12 20:01

jtl999


People also ask

How do you draw an image in Python pygame?

Create a Image surface object i.e.surface object in which image is drawn on it, using image. load() method of pygame. Copy the image surface object to the display surface object using blit() method of pygame display surface object. Show the display surface object on the pygame window using display.

Can pygame use PNG?

Pygame is able to load images onto Surface objects from PNG, JPG, GIF, and BMP image files.

Can you draw in pygame?

In this PyGame and Python programming tutorial video, we cover how to draw shapes with PyGame's built in drawing functionality. We can do things like draw specific pixels, lines, circles, rectangles, and any polygon we want by simply specifying the points to draw between. Let's get started!


2 Answers

This is a typical layout:

myimage = pygame.image.load("myimage.bmp")
imagerect = myimage.get_rect()

while 1:
    your_code_here

    screen.fill(black)
    screen.blit(myimage, imagerect)
    pygame.display.flip()
like image 190
joaquin Avatar answered Oct 25 '22 13:10

joaquin


import pygame, sys, os
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((100, 100))

player = pygame.image.load(os.path.join("player.png"))
player.convert()

while True:
    screen.blit(player, (10, 10))
    pygame.display.flip()

pygame.quit()

Loads the file player.png. Run this and it works perfectly. So hopefully you learn something.

like image 37
John Riselvato Avatar answered Oct 25 '22 12:10

John Riselvato