I am having trouble exiting my infinite loop in pygame - python

Code
This is my little game of pig. I only got it coded for 2 players. I have ran multiple scenarios and it seems to work out good.
I just can't seem to exit the while loop
I am having trouble at line #109.
I set it to break, but i believe it may be break from the small if statement, which I don't want.
I am wanting to break from the large while loop ( game loop ) line #100
I also want to use pygame to add some graphics of dice i have created.
I was wondering if I can keep my code as is and just modify it to work with pygame easily or do I have change the entire code to work with pygame?
I just want to do a simple interface
It will be similar to this : http://cs.gettysburg.edu/projects/pig/pi…
but I will add in dice, simulation of dice rolling 2d, sounds, a win state graphics and sounds, and input, mainly mouse toggle.
Any tips on how to do the pygame portion is much appreciated.
I have read many tutorials on pygame, but I just don't feel confident to get it to work with my own game.
I have made a ball bounce, but I really don't understand what some of pygame's code mean.
I am a little confused on display screen vs background
screen would be pygame.display.set_mode((some size))
then background will capture the exact size of the screen, but not really sure.
I have figured out how to put dice on the screen, and change face every half a second, but that's it.

nice little program. it seems to work fine for me (break exits for and while loops, not if).
when you play the game it's a little confusing because the check to see if someone has won comes only after the other player plays and then you enter 'hold'. perhaps that is what made you think it wasn't working? it might be better (i don't know the exact rules of pig) to put the check for winning right after the code that runs on 'hold'?
sorry can't help with pygame; just wanted to post to say it seems to work.
also, if you wanted, you could put the two players in an array:
players = [player1, player2]
and then the current player would be:
players[player_turn-1]
and you could use the same code for either player, duplicating the logic in the main loop. so the code would be more like:
if players[player_turn-1].winner():
...

#Make a Variable set it to "False".
#= means to assign something to something.
#== means equal to.
#Example in a if statement if something == to something do something.
test = False
#This is the loop.
while test == False:
#Put Code Here.
#Put a If Statement `enter code here`to stop loop.
if #Parameters:
test = True

Related

How to render blocks using columns for a game?

I am using a website called pixelpad.io to make my game, which is in Python. I am trying to make a simple platformer where the player can move horizontally and jump on blocks. Because of this website, I have to specify the co ordinates of each block for each level I make. Since this is all on a browser and I spawned all of the blocks in at the start of the game, the fps has been running slow. A friend of mine said to figure out the x co ordinate of left side of screen compared to the player along with the co ordinate of the right side. He then said to do some math to figure out which columns are visible, and generate the blocks in those columns. I somewhat understand his explanation but I am still confused on how to code it. How should I store and use all the block information for each column since the level is preset? I have a couple types of sprites for different blocks, so I'm not too sure how to store that information either. When the player is outside of a column that was rendered, does it destroy itself? I need an explanation for this, pseudo code, or an easier alternative to use.
I'm not sure what API you are using but I'm guessing it's not pygame. However, this pygame tutorial about optimization might help. I found the basic theory of it very useful in learning how to optimize my pygame game projects.
https://youtu.be/s3aYw54R5KY

Pygame window freezes sometimes

I created A star algorithm visualization in pygame, and pygame window freezes sometimes (when algorithm is working), I know that this algorithm is not the most optimized one, but I think that the algorithm isnt causing this issue. Please help me, because its quite annoying, and i dont know what causes this issue.
Link to code -> https://github.com/DeathEyeXD/PythonProjects/blob/master/aStarVisualization.py
Please paste relevant part of the code here and show us what you have tried so far.
I assume that you have a main pygame loop running at a certain frequency. When you have a function calculating things in the main loop that takes a considerable amount of time, it will cause the game window to freeze. This is because it cannot reach other things (like your event function) at the same time as it is calculating the path.
With A* algorithm you can make it nicer by drawing the progress (draw the neighbors and checked cells for example). I did a project like this previously which works nicely. You then call your draw function within the A* function at relevant places and you won't get the freeze issue.

How to prevent time delay/racing in python?

I'm trying to implement a script which will play the game snake using the a* algorithm. The game was given to me by someone else (my teacher) and I have to write an working script that plays the game as good as possible, using this exact algorithm or some variation of it.
Now, I have written the algorithm and tried various simulations of it and it turns out that it works just fine, however since the game is an .exe I have to take a screenshot of the game screen and transform that into input data for my algorithm from that I construct a path and give commands to the snake using pyautogui.
So to summarize, the algorithm:
Take screenshot
Calculate path and moves
Move towards goal
Repeat until snake dies or certain score is reached.
The problem which I face is that the snake has a certain time which it takes to make 1 move and I delay the key press accordingly to that time, however when I reach the goal I have to repeat all the steps and steps 1-2 always take about 45-55ms to complete and in that time my snake will end up dying because it didn't react quick enough. So what would be a good way to prevent this?
The solution I came up with, works sometimes, but a time delay will form and the snake will either make one move too early or too late at some point which might be deadly:
def simulate_movement(moves, speed):
for move in moves:
text, pause = move
interval1 = round(pause * speed, 1)
if move == moves[-1]:
interval1 = 0.05 # I speed up the final move because steps 1 and 2 take about 0.05ms
pyautogui.press(text, interval=interval1)
Example of what the delay might cause:
The snake in the image has died, however the path which was correct was provided for it, but because it didn't react in time (because of mentioned delay) it bumped into the corner. Is there a way to for example make some code block which takes between 50-60ms to make it run 100ms so that is do nothing until that time passes, because that way I could get rid of the delay by subtracting 100ms from the final move and waiting 100ms until next moves are calculated.

Making items appear from obstacles in game?

I'm making a game using Pygame where there are random obstacles on the screen (like boxes). The boxes can be removed by the player when they place a bomb next to it. I have some items that I would like to have randomly appear in place of the box if it is removed. I'm not sure how to go about this logically. Does anyone have any tips? No code is necessary (but helpful), I just want some steps logic-wise to get me started.
In order to have the box removed you need to call a ~destructor function on it, which would remove the image of the box and so on correct?
Take advantage of that and create a function that chooses which item to spawn (could be random, up to you) in the position where the box used to be.
Then call this function at the end of the destructor. That's how imagine it working.

How to use multi touch in a python paint program

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.

Categories