how to add interface in python, with pygame - python

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

Related

Pygame delete non sprite objects

So Im trying to make an upward scrolling platformer, and I need a way to delete the score counter and hide the death screen when a player dies and restarts. However, those objects aren't sprites. Is there still a way to delete them?
this is just an example because you have not provided any code :'(
if not ondeathscreen:
#draw your deathscreen
if not inGame:
#draw your score counter
you have to personilize this code with your game but hopefully it helps :))).

Pygame GUI with two "screens"

is there any way to create a game were there are two background colors where one square is smaller than the other. In the background, I am thinking of being able to place buttons and so on. I have a game that works fine but I would like to develop it. As I have no knowledge about how to do, I have not tried it.
Pygame won't allow you to have more than one screen per application, if that is what you are asking. You can check the pyglet library for that: http://pyglet.org/ -
I can't figure out if you have any further doubts from your text, however - you will have to be more specific.
If it is about having a sub-section of the window as the action area of the game, and an outer margin where to to place controls, yes, that is just straightforward drawing with the provided calls.

Getting position of multiple touches with PyGame on mobile [duplicate]

so im building a simple paint program in python as a project, using Pygame it works by basically drawing a stream of circles when the mouse gets pressed and you drag it around the surface, its got a couple other little things going on but the thing i wanna ask is, is there a way to change the singular mouse input you know mouse.get_pressed to either multiple mouse inputs at one time or a multi-touch input into the point list that's streaming the circles.
running= True
while running:
if buttons[0] == True:
x,y = pygame.mouse.get_pos()
if x> PA+AS:
xShift = x - PA - AS
pygame.draw.circle(pArea,DRAW_CLR,(xShift,y),BRUSHSIZE)
pygame.display.flip()
so this is the part of the code i really want to change more or less. just so that instead of just one mouse, i could use my touchscreen to draw with maybe two finger
Latest versions of pygame support multitouch input (and I believe also gestures).
The events which control it are pygame.FINGERDOWN, .FINGERUP and .FINGERMOTION. My understanding is that they work like mouse inputs, but can be multiple and can be distinguished by means of an event.finger_id property.
An example can be found here:
https://www.patreon.com/posts/finger-painting-43786073?l=fr
You're going to have a difficult time if you insist on doing this in PyGame. Your best bet for multitouch in Python is Kivy, which was a very solid framework a few years ago when I used it and appears to have only gotten better since.
The disadvantage of switching to Kivy is a more complicated API, but this tutorial seems spot-on for what you're trying to do.

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

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