How to draw images and sprites in pygame?
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()
How can you draw more detailed/smoother images in pygame?
Use pygame.transform.smoothscale
instead of pygame.transform.scale
:
img = pygame.transform.scale(img, (500,500))
img = pygame.transform.smoothscale(img, (500,500))
While pygame.transform.scale
performs a fast scaling with the nearest pixel, pygame.transform.smoothscale
scales a surface smoothly to any size with interpolation of the pixels.
For an even better result, you may want to switch to a vector graphic format such as SVG (Scalable Vector Graphics).
See the answers to the question SVG rendering in a PyGame application a nd the following minimal example:
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
pygame_surface = pygame.image.load('Ice.svg')
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill((127, 127, 127))
window.blit(pygame_surface, pygame_surface.get_rect(center = window.get_rect().center))
pygame.display.flip()
pygame.quit()
exit()
Drawing shapes versus rendering images?
It is easer to use pygame.draw.rect()
or pygame.draw.polygon()
(because you don't need to know how to use GIMP or InkScape :) ) but you have to draw it on another pygame.Surface()
(to get bitmap) and than you can rotate it, add alpha (to make transparet) and than you can put it on screen.
You can create function to generate images (using Surface()) with all shapes in different orientations at program start. If you will need better looking images you can change function to load images created in GIMP.
Try every method on your own - this is the best method to check which one is good for you.
By the way: you can save generated images pygame.image.save()
and then load it. You can have all elements on one image and use part of image Surface.get_clip()
Python pygame converting drawing to an image
Create a surface with the same size as the display and the background color:
grid_surf = pygame.Surface((width, height))
grid_surf.fill((255,255,120))
Draw the grid to this surface:
def drawgrid(surf):
for x in range(0, width, 40):
pygame.draw.rect(surf, (0, 0, 0), (x, 0, 2, height))
for y in range(0, height, 40):
pygame.draw.rect(surf, (0, 0, 0), (0, y, width, 2))
drawgrid(grid_surf)
And blit the surface in the main application loop instead of drawing the grid and background:
screen.blit(grid_surf, (0, 0))
Example code:
import pygame
pygame.init()
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('grid')
def drawgrid(surf):
for x in range(0, width, 40):
pygame.draw.rect(surf, (0, 0, 0), (x, 0, 2, height))
for y in range(0, height, 40):
pygame.draw.rect(surf, (0, 0, 0), (0, y, width, 2))
grid_surf = pygame.Surface((width, height))
grid_surf.fill((255,255,120))
drawgrid(grid_surf)
running = True
while running:
screen.blit(grid_surf, (0, 0))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running == False
pygame.quit()
PyGame - how to draw images in a loop
You are constantly modifying the rect of the same object. That can’t work.
Instead create a list of coordinates and blit the same image to different destinations based on that list.
Related Topics
How to Check If a List Is Empty
Prevent Scientific Notation in Matplotlib.Pyplot
How to Fix "Runtimeerror: Package Fails to Pass a Sanity Check" For Numpy and Pandas
Why Is Python Running My Module When I Import It, and How to Stop It
How Are Python'S Built in Dictionaries Implemented
How to Find All Matches to a Regular Expression in Python
Extracting an Attribute Value With Beautifulsoup
Changing One List Unexpectedly Changes Another, Too
Speed Up Millions of Regex Replacements in Python 3
Which Python Memory Profiler Is Recommended
Importerror: No Module Named 'Pygame'
How to Get a Substring of a String in Python
Adding Python to Path on Windows
How to Check If a String Is a Substring of Items in a List of Strings
Why Can't Python'S Raw String Literals End With a Single Backslash