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

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:
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

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)

Simplest way: Create a group for each 'layer'.
import pygame
front_sprites = pygame.sprite.Group()
middle_sprites = pygame.sprite.Group()
background_sprites = pygame.sprite.Group()
Then, just draw them all onto the screen in the following order:
background_sprites.draw(screen)
middle_sprites.draw(screen)
front_sprites.draw(screen)

Related

Pygame zooming screen

im writing a game right now with 32x32 textures, i have a problem because my screen window is too small, is there any solution? Can i "zoom" my whole game?
Now it looks like this and exactly what i want is to make whole screen bigger and zoom to see only like 5% of map.
Of course, if i make my screen bigger it not gonna fix my problem.
Maybe i should add another camera or something?
I think I made myself quite clear.
Thanks!
First, you can definitely zoom as you are saying.
screen.blit(pygame.transform.scale(display, (int(width), int(height)))
You can resize the screen:
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT),pygame.RESIZABLE)
def resize(event):
global WINDOWWIDTH, WINDOWHEIGHT
if event.type == pygame.VIDEORESIZE:
WINDOWWIDTH = event.size[0]
WINDOWHEIGHT = event.size[1]
return pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), pygame.RESIZABLE)
And have the display the actual size you want to draw to:
display = pygame.Surface((300,200))
However, scaling surfaces is really bad as pixel art isn't great at scaling. An alternative would be a camera or switching off of pygame which would make everything you are trying to do way less complicated. I would recommend rubato for a start. It will do all this work for you, and you can decide how big you want your actual grid (display) to be with one variable.
Good luck! Feel free to comment if you want any extra help.
One way you can fix this is by shrinking the size of the screen itself:
#you can adjust the width and height in game until it seems the right fit
screen=pygame.display.set_mode((WIDTH,HEIGHT))
And if you want, you can set the window to Fullscreen:
screen=pygame.display.set_mode((WIDTH,HEIGHT),pygame.FULLSCREEN)

PyGame: How can I use a different part of my background image when my sprite goes past the screen?

Like for example my game screen is 600 by 600, and my image is 600 by 1500. How can I make it so that my background moves to another part of the image when my sprite goes off screen, so it seems like I'm moving through but I'm really not?
Sorry if I seem confusing.
Thanks.
From the pygame documentation on blit, you can pass in an area of your background image that you want to draw.
My pygame is a bit rusty, but you should be able to do the following:
blit(background_image, destination_surface, ((left_x_offset, 0), (right_x_offset, 0))
The first argument is your background image (the 600 x 1500 one), the second argument is the surface you're drawing everything to, and the third argument is the area of your background image that you want to draw to the destination surface.
Since you want to move the background in relation to the character, I would set left_x_offset to the position of the character minus half the width of your screen, and right_x_offset to the position of the character plus half the screen size.

How to make an invisible sprite in pygame? (python)

to elaborate, I'm currently creating a maze layout using wall sprites. however, I want the maze to be invisible when I am actually playing the game, while still able to be able to have collision detection. Is this possible in any way? Thanks.
Are you using images to show your walls? Because if you are, you can use a program called paint.net to create a transparent sprite. Just create a file with the desired dimensions and use the eraser tool to make the whole thing transparent (click on the little squiggly arrow up at the top to make it easier). Save it as a png in your pygame folder and you have a transparent square.

how to add interface in python, with pygame

Help! I am new to programming! I am trying to make a monopoly game. My program so far creates player and card objects, these objects have some values stored.
I am trying to use pygame to implement some kind of animations and create a menu bar that pops up on the pygame screen, but I don't know how to get started. So far all I have is a pygame background screen, I want to know how I can place a small rectangle on the pygame screen given position, depending on the players object's position value. I need to know how I can have all that code I have written just for the game logic, and have it run along side with this pygame stuff.
I really need to know how to get started. If anyone is willing to spend some time with me, to get me going in the right direction, I would really appreciate it, thanks!
Reading over some pygame tutorials will certainly help.
The flow of your program will eventually look something like this:
# This is intentionally simplified pseudocode, but at the bottom-most level most
# simple games have a similar structure to this.
#Construct your game objects
players = [Player(args) for n in range(num_players)]
cards = loadCards() #or however you load your cards (files,database,etc)
board = Board()
#initialize your display context
pygame_screen.init() #or whatever is the correct syntax for Pygame
# Main game loop
while 1:
check_inputs() #get key/mouse input inputs for this frame
handle_inputs() #handle each input that has occurred during this frame
update_gamestate() #update the state on the game (who's turn, etc)
update_display() #update pygame window graphics (draw stuff, flip display)
cleanup()
As for the details on how to draw rectangles and whatnot, you should read about the various draw functions here

How would you draw multiple screens in PyGame (in only one window)?

I'm trying to port over some code from Javascript to Python, and I'm having trouble even figuring out where to start. I've looked at a good dozen tutorials for PyGame, but none of them seem to click for me. So, I was hoping to get a quick example here, or at least a point in the right direction.
I'm wanting to make a number of screens I can switch back and forth between, depending on what the user is doing at the time, and even display two side by side. At the moment, all I've got is some Javascript that draws random circles onto the screen. The PyGame logic is the only thing I'm having trouble with.
Here's my javascript for reference.
You can create a Subsurface for each subscreen that you want to create.
Then you may treat each as if it were a full screen / single surface, yet they still reference the original screen.
Pygame is a wrapper for SDL. SDL uses a surface to represent a bitmap, or anything that can be drawn on screen. With the pygame.display.set_mode((w,h),0,d) you can get the surface, or the whole canvas. You can then draw or blit the other surfaces and then call flip(), to show changes. If you wish to have a few screens, you could have a current state number, and blit the screens accordingly.
For example:
if(current_state == MAIN_SCREEN):
drawAll(screen)
else
drawEnemiesOnly(screen)
you could change the screens with the number keys:
for event in pygame.event.get():
if event.type == KEY_DOWN:
if(event.key == K_1):
current_state = 1

Categories

Resources