Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame, is there a way to make "screen layers"?

I am working on a Tank game as a school project and I am trying to make it as user-friendly as possible (with things like customizable keybindings, display settings). However, I don't want the game window to lose it proportions so I figured I would add borders to the sides of the game window in order to support wide screens.

I've attached an imageto illustrate what I'm looking for:

enter image description here

So for this to work, I would need a way to make "screen layers". The base layer is the whole screen with some graphics on the sides added to it and a font displaying score. And then a second layer would be rendered in the middle of the screen which will be the game window and its width and height will be obviously smaller than the base layer.

At the moment, the game is rendered from the top left corner of the screen until all the tiles have been rendered out. So that is why I figured it would be the best to make the tiles render out on a separate surface positioned in the middle of the screen surface.

Would really appreciate some tips on how I can make this possible! :) I don't have that much experience in Classes and pygame, so it would be awesome if you could give a fleshed out description as well! :D


1 Answers

Just draw your different stuff on different surfaces.

For example, create a Surface for your game window (let's call it game_surf), and draw your tanks and bullets etc. on that new Surface instead of the pygame main window (which is just another Surface).

# the main window
screen = pygame.display.init() 

# just a regular surface
game_surf = pygame.surface.Surface(width, height)

...
# in your main loop
    # or however you draw your sprites
    your_sprite_group.draw(game_surf)

Then blit it onto the screen

screen.blit(game_surf, the_position)
like image 61
sloth Avatar answered Oct 30 '25 06:10

sloth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!