Images
In PyGame Images are stored as Surface. Also display can be treated as ordinary image. It simplify screen management.
PyGame uses odd coordinate system. top left corner is (0,0) top right (640,0) bottom left (0,480) and bottom right is (640,480) if window is (640,480) of course values can be different and you set them when calling pygame.display.set_mode().
Drawing on the screen
To draw something on the screen we must first load image from file. Then blit it on display surface and at the end order PyGame to draw everything on the screen.
Blitting is a process of putting one image onto another e.g. we will put our hero on the screen every frame in a bit different positions to create illusion of motion.
display_surf.blit(image_to_paint, (0,0))
It will paint image_to_paint in (0,0) point but you can paint it everywhere inside display_surf.
Loading images
For loading images PyGame have pygame.image module. To load a particular image use:
image_surf = pygame.image.load("myimage.bmp").convert()
This line means load "myimage.bmp" and put it in new Surface "pygame.image.load("myimage.bmp")", than take
this Surface and convert to new with best pixel&color format and assign it to image_surf.
If you wan't to make your games os independent use os.path.join("folder_where_your_data_are", "file_name_of_image") to create os specific path to file e.g uder linux it will be "folder_where_your_data_are/file_name_of_image"
Sum up
import pygame from pygame.locals import * class App: def __init__(self): self._running = True self._display_surf = None self._image_surf = None def on_init(self): pygame.init() self._display_surf = pygame.display.set_mode((350,350), pygame.HWSURFACE) self._running = True self._image_surf = pygame.image.load("myimage.jpg").convert() def on_event(self, event): if event.type == QUIT: self._running = False def on_loop(self): pass def on_render(self): self._display_surf.blit(self._image_surf,(0,0)) pygame.display.flip() def on_cleanup(self): pygame.quit() def on_execute(self): if self.on_init() == False: self._running = False while( self._running ): for event in pygame.event.get(): self.on_event(event) self.on_loop() self.on_render() self.on_cleanup() if __name__ == "__main__" : theApp = App() theApp.on_execute()
I've changed only on_init and on_render.
If you like you can use this file. Which btw show glider from Conawy's Game of Life.
Multiple images in one file
Imagine that in our game we have 100 types of ground is this mean that we have to create 100 files?
NO!! just put therm together and order PyGame to cut out only those you need at the moment.
use:
image_surf = pygame.image.load("one_big_file.bmp").convert() # ........ # and for drawing only part of it: display_surf.blit(image_surf, (0,0) , rect_containing_coordinates_to_draw)
Zipped source code:
Big thx for catching bugs and voicing their propositions to:
- Douglas Rae
- panthar
- LlucPot
- Thomas
This is actually such a shitty tutorial, you should feel bad
^^ troll
^^ rabbit
what the fuck is happening here just fucking stop you dipshits before i kill you
Oof ouch that E D G E
Even though this guy is a troll, he's right. This tutorial is really shitty, and not at all newbie-friendly. There's a lot of unnecessary and unexplained code. I remember in the first tutorial, something like "simple" screen rendering or whatever, where there were dozens of different functions and shit, none of which was explained in any coherent way. If you're wondering how to bring up a simple screen, it can be done in a few lines of code, and I'm not sure why this guy is bringing up all this garbage.
import pygame
screen = pygame.display.set_mode((640,400))
while True:
pass
That's literally all it takes.
I agree with you; a lot of what this tutorial has in the script isn't explained. I do like the concept of using a class to have the game in, even though there is no real need for it. It's not like we need to make to games that are exactly the same in one script.
This is just explaining a general setup for pygame which is understandable if you're already familiar with python, which this tutorial assumes you are.
Also it's demonstrating an object oriented approach, your few lines of code would be fine but it will quickly become hard to manage the complexity of your game and add to it, meanwhile the object oriented approach has got his framework setup and can easily and quickly add content and features to the game.
Seriously? Do you know anything of setting up structure for larger programs, or OOP in general? You might want to learn before making such comments.
do u have the link to a better one?
If you want to make games just use godot engine. it's open source and light weight but still quite a powerful engine. It's scripting language is pretty similar to python or you can use C too.
FUck u
oh doe foe that one hurts
The code works. Wish you could have shown how to loop load different images too.
But cool. thanks.
If you want to load multiple images in a loop, you'll probably want to use a for loop to get the job done. If there's a specific set of different images you want in a specific order, you might want to try something like:
for i in range(number_of_images):
if i == 0:
image1 = pygame.image.load etc etc etc
if i == 1:
image2 = pygame etc etc etc
why not store the images in a dict and access them in a more straightforward way?
sprite_dict = {
"hero" : "hero.png",
"badguy" : "orc.png"
}
imgs = []
for key in sprite_dict :
imgs.append( pygame.image.load(sprite_dict[key] )
…
You could dynamically create your sprite_dict and load from what's in there each loop.
Useful Advice! Thanks.
nice tutorial, short and concise. Thank you.
It's a good tutorial. I've been reading a little on the pygame documents. Which is long and good. But here, it's what helps me to summarize what I've got till now.
Great tutorial. Thanks for sharing this with us; Jesus Christ Bless! :)
Wonderful tut for game programmers who are new to python!
How to set the image size
i am the coolest guy ever, this is a good tutorial, and i am cool,