Pygame: Displaying number of moves, timer and a win / fail screen - python

I am making a kind of puzzle game and it's been suggested that I use Pygame. I have looked at some tutorials but I am unable to get certain things to show.
Number of moves - I'm basically looking for something that will count how many times the numbers 1 - 6 are pressed.
Timer - I'd like this to start when someone presses a key and stop after a condition has been fulfilled.
Win / fail screen - I'm looking to have something pop up saying the user has won or failed depending on if certain criteria have been met.
Any help on getting these displayed would be greatly appreciated.

Firstly get familiar with pygame font module : http://www.pygame.org/docs/ref/font.html
You will need a way to display string and numbers to the user. Simply make a font object, and render surfaces with the strings, to later blit them like any other bitmap you had.
Also i suggest to look into the time module: the pygame docs are really helpful : http://www.pygame.org/docs/ref/time.html. Here you will use this module to create a small class Timer, with a Clock object to stop and reset, and update functions. In the update functions, you can add the delta(the difference between the last and recent call of tick()) to the overall time or simply depend on the functions given in the time module. You can call stop when a condition is satisfied.
Last, for the screen, you may want to divide your game/app into states, where the game state is only rendered and looped when a play flag == true. When the game is over, you can switch the state to win/lose state to display the information and probably a prompt if the player may want to play again.

Related

Is there a way to make a function wait for image to show up then clicking the image while on display using PyAutoGui (Python)

I'm currently making a BTD6 Auto Farmer what this means is that it will farm the game for me and click on the screen and play the game for me and complete the maps and stuff so I can get rewards that do not give me a competitive advantage because the game is single-player.
And to complete this bot I'd have to wait a certain amount of time and then see whenever a button shows up on the screen and to accomplish this I need to make a function that will wait for an image to show up on the screen and if the image shows up the function should press that image and that's all.
I tried many things and couldn't get it to work, here is my code that doesn't work:
def click_image(img):
x = pag.locateCenterOnScreen(img, confidence=config.OPENCV_CONFIDENCE)
pag.click(x)
This didn't work and I'm not surprised because the code creates the variable then tries pressing the variable there is no check or wait and I don't know how to implement that and that's what I'm asking for, that's the answer I'm looking for :)
It seems like you want to perform an asynchronous task, you should look into the python package asyncio.

How to stop tkinter looping indefinitely?

I'm trying to add a tkinter GUI to a simple text-based game made in python but I'm struggling with the game loop, which either runs infinitely or not at all. I would like the game to ask the player what they want to do, then do something based on that, then return to asking the player what they want. The problem I'm having is instead of calling the function once, it calls it repeatedly.
while playing is True:
getAreaDesc()
requestPlayerMove()
if action == 1:
explore()
elif action == 2:
travel()
elif action == 3:
rest()
window.update_idletasks()
window.update()
I'm fairly sure I know what is causing the problem. As I understand it, this is because the value for action is determined by a tkinter button (with a command setting the value for action) so as soon as a button is pressed, it calls the relevant function. Then it checks again but as the action is still the same, it calls the function again. And so on and it doesn't stop until a different button is pressed and a different. I think it's like a lightswitch in the sense that once it's on it stays on, even though the switch is only flicked once.
However, even if I am right about why it calls the functions continuously, I can't think of a way to prevent that occuring which doesn't simply stop the loop.
I've tried resetting action as part of the functions, but that causes it to do nothing beyond ask for player choice. Similarly, trying to nest the if/elif parts comes to the same result, and using after() changes nothing but the speed at which it loops relentlessly. And those were my good ideas. I tried putting input() back in to wait for the player and that went about as well as you could imagine.
I'm sure it's something daft I'm missing, hopefully not offensively stupid, but I'm at a loss and any help would be very appreciated. Thanks for your time reading this, if your patience lasted this far.

How to do a function at the same time with the loop?

I am trying to run the turtle that can move left and right by arrow key, and at the same time two object call harm and benefit fall down from top of the screen. However, I can not do both of them at the same time
I running by repl.it.
def run():
screen.onkey(left,"Left")
screen.onkey(right,"Right")
while True:
benefit()
harm()
run()
Can someone help me with this?
Not sure what framework you're using to drive your app, but most likely you want to split what's going on to single-frame actions. Find how to execute something each time a new frame is displayed and in that code execute something that moves your objects just a bit. For falling objects you can do that by either updating the y coordinate by some constant, or by (this_frame_time - last_frame_time) * speed to keep the movement smooth.

Python Move Mouse in Active window

Firstly, there are a couple of similar questions posted on this. But they are not complete questions, and no one has really answered them all. I am using Python to send input to a game active in widow modes using "ctypes" package. The original idea is mentioned here
In particular, my codes are:
x=500
y=400
for i in range(10):
time.sleep(4)
if i > 0:
x=x+17
pos=(x,y)
#ctypes.windll.user32.SetCursorPos(x,y) # this does not work in game but
#outside the game window
ctypes.windll.user32.mouse_event(0,x,y,0,0)
print i
In order for the "mouse_event" to work, I tried inputting hexadecimal values but can't get it to move consistently. I tried setting up separate class & functions to import, then calling the function. The function works outside the game window but not inside the game window.
The separate class & functions are based on this question
Thank you for your help in advance.

Creating a starting player with pygame

I got a problem right now. I need to create a starting player menu with the options on how many players and giving those players a color. Also whenever a player has a color, the color will get deleted from a list.
Can any1 help me :(
You can treat menu almost as separated game - with own mainloop, own functions and classes - but in the same file with rest of code.
And use pygame.init(), pygame.display.set_mode() only once - when you init game.
Latter you can find better solution (but more complicated) with only one mainloop - Finite-State Machine "FSM"

Categories