Pygame: After I let go of the button action keeps on going - pygame

I have already made a few projects, but this time I ran into a problem.
I made a game type when the arrow goes across a bar and you click a button to stop it and you aim it as close to the middle.
The button you need to press is randomized (w, a, s, d). When a button comes 2 or more times in a row, the other times it stops the arrow in the first frame, like it was being pressed the whole time. It's the same when I am done with the arrow mini-game and I go to a 2D RPG like game. It keeps on moving in the direction that I last pressed like the button is stuck.
When I press it again it stops and I can move freely.
I can include the code if you want to look but it is long and complicated so I don't want to make you look through the whole thing if not necessary.
If you can help or have any questions write them down, thanks.
Edit:
I am including a part of the code in which the arrow mini-game is going on, if you want a full version it is about 200 lines. Thanks.
while borba:
if enemyhp<=0:
break
crtanjeploceborbe()
crtanjeenemyhealthbar(enemyhp)
crtanjeneprijatelja((300+500)/2-100/2,10,neprijateljnb)
pygame.display.update()
mis=pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
if event.type==pygame.MOUSEBUTTONDOWN:
if mis[0]<fightbuttonpos[2] and mis[0]>fightbuttonpos[0] and mis[1]<fightbuttonpos[1] and mis[1]>fightbuttonpos[3]:
fightmeterx=random.randint(actionspaceavailablex[0]-1,actionspaceavailablex[1])
fightmetery=random.randint(actionspaceavailabley[0]-1,actionspaceavailabley[1])
button=random.choice(["w","a","s","d"])
poztipke=fightmeterx-45
brzinatipke=2.5
natezanje=True
while natezanje:
if poztipke>=fightmeterx+380-45:
poztipke=fightmeterx+380-45
pygame.time.wait(2500)
dmg=5
break
for event in pygame.event.get():
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_w:
buttonpress="w"
elif event.key==pygame.K_a:
buttonpress="a"
elif event.key==pygame.K_d:
buttonpress="d"
elif event.key==pygame.K_s:
buttonpress="s"
if buttonpress==button:
if poztipke+45<=fightmeterx+104 or poztipke>=fightmeterx+344:
dmg=5
elif poztipke+45<=fightmeterx+190 or poztipke>=fightmeterx+302:
dmg=10
elif poztipke+45<=fightmeterx+261:
dmg=20
else:
dmg=40
pygame.time.wait(1500)
borba=True
natezanje=False
crtanjeploceborbe()
crtanjeenemyhealthbar(enemyhp)
crtanjeneprijatelja((300+500)/2-100/2,10,neprijateljnb)
crtanjefightmetera(fightmeterx,fightmetery)
if button == "w":
crtanjewtipke(poztipke,fightmetery-90)
elif button == "a":
crtanjeatipke(poztipke,fightmetery-90)
elif button == "s":
crtanjestipke(poztipke,fightmetery-90)
else:
crtanjedtipke(poztipke,fightmetery-90)
pygame.display.update()
brzinatipke=brzinatipke*1.015
poztipke+=brzinatipke
enemyhp-=dmg

I never change back the pressed button so it remembers the button pressed from the last minigame(loop)

Hello leon lukacevic,
I suggest adding if event.type == pygame.MOUSEBUTTONUP with the required action, this will solve your issue.
Not related to your question, but you called for event in pygame.event.get() again inside the loop. This may create a problem in the future.
Please click the tick if you found my answer helpful :) Thanks

Related

Pygame resizable window, surface not updating appropriately

I am using pygame to draw graphs with some different graph drawing algorithms. Previously I had the size be constant but now I want it to be re-sizable. I was able to get the resizing to work in certain cases. For example, it works when I press the resize window button at the top of the client and properly fills the surface in. However, if I try to resize the window by dragging the corners out I have to do it very slowly otherwise it wont work. Is there a way to have pygame update faster with me dragging. I've looked at other examples but was unable to make it work in my case, maybe cause I'm using thorpy too?
The main-loop event code is:
playing_game=True
clock = pygame.time.Clock()
while playing_game:
pygame.event.pump()
event=pygame.event.wait()
if event.type == QUIT:
playing_game = False
pygame.display.quit()
break
elif event.type == VIDEORESIZE:
myMenu.display = pygame.display.set_mode(event.dict['size'], HWSURFACE|DOUBLEBUF|RESIZABLE)
myMenu.resize(200, event.dict['h'], event.dict['w'], event.dict['h'])
myMenu.itemMenu.blit_and_update()
pygame.display.update()
clock.tick(60)
myMenu.itemMenu.react(event)
The entire code can be found at: https://github.com/CamiloJac/GraphVis
Working properly when resize window button is pressed
Working incorrectly when I drag the edges of the screen
I suspect the combination of pygame.event.pump() and event=pygame.event.wait() is causing the event to be lost.
The event.wait() is only processing a single event, then the pump() tells PyGame to handle the remaining events. I'm guessing some kind of syncopation is happening, and the resize is being lost.
I have this sort of re-sizing working with processing all events:
SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
...
while ( not done ):
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
elif ( event.type == pygame.VIDEORESIZE ):
WINDOW_WIDTH = event.w
WINDOW_HEIGHT = event.h
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), SURFACE )

How to make a toggle type button in pygame?

I'm making a game with pygame, but I have a problem that button does not work as a toggle.
while True:
mousepos = pygame.mouse.get_pos()
mouseclk = pygame.mouse.get_pressed()
game.fill((0,0,0))
game.blit(cic, (0,0))
if mouseclk[0] and 1227 > mousepos[0] > 1120 and 485 > mousepos[1] > 413:
while True:
game.blit(pbut_rgm84, (1120,413))
pygame.mixer.music.load("sounds\se_beep.mp3")
pygame.mixer.music.play(0)
current_weaponinf = font.render(current_weapon[1], True, (255, 0, 0))
game.blit(current_weaponinf, (930,45))
else:
game.blit(but_rgm84, (1120,413))
This is my code for the button. As you know, If I click on that button, image of the button will be changed and sound will be played. But it will happen while i'm pressing on the button. So, I want to the button to be toggle type. Do you have any ideas on it?
EDIT 1: Sorry for the ramble. The answer is in the first part. The rest is just some notes on a more standard way to do what you asked for in PyGame. After all, the answer is not just for the OP, but everyone who visits this page.
EDIT 2: Notice also I had to change the code to lower down the FPS to 5 for this to work fine. The way you checked for a click meant that the mouse button was down for about 30 - 40 frames every click on my machine, hence resulting in 30 to 40 button toggles when only one was needed, so I had to lower down the FPS to 5 in the code. This is another reason why you might want to use the other method I described for event handling.
Answer:
I honestly appreciate the simple logic you used to check if the mouse click was in range of the object being pressed.
I could extend your code slightly to get the "toggle" type button you want (i.e, on a click, the image and sound changes. Click again, and the image and sound go back to normal).
However, I also would like to show you some better ways of dealing with mouse click events and click detection available in the Pygame module to improve your code. If you are interested and want to know what I mean, please read on after the code below. If you just wanna take the code and leave, I totally respect that (it's something I did a lot as well).
Here is your code modified to match your "toggle" button requirements (if I understood what you want to do right). Replace this with the part of your code you showed in your answer, but be aware that this may not work as I don't have your full code or the images / soundtracks you used :
toggle = False
pygame.mixer.music.load("sounds\se_beep.mp3")
pygame.mixer.music.play(-1)
pygame.mixer.music.pause()
clock = pygame.time.Clock()
while True:
mousepos = pygame.mouse.get_pos()
mouseclk = pygame.mouse.get_pressed()
game.fill((0, 0, 0))
game.blit(cic, (0, 0))
if mouseclk[0] and 1227 > mousepos[0] > 1120 and 485 > mousepos[1] > 413:
if toggle:
toggle = False
pygame.mixer.music.pause()
else:
toggle = True
pygame.mixer.music.unpause()
if toggle:
game.blit(pbut_rgm84, (1120, 413))
current_weaponinf = font.render(current_weapon[1], True, (255, 0, 0))
game.blit(current_weaponinf, (930,45))
else:
game.blit(but_rgm84, (1120,413))
clock.tick(5)
A Better Way: Handling Events
Handling mouse click events and in fact all other events is simple with what is called an event loop. Although there is a little bit more to it than this, for now we can say than an event loop checks for certain events triggered (such as key press, mouse click, quit game events) at every frame, and then triggers a callback function.
You already have one in your code! - At every loop in your while loop, you check if the mouse was clicked using mouseclk = pygame.mouse.get_pressed()
However this is incomplete and is not the standard way to implement event loops using the Pygame module. Here is how you do it (read the comment above each line to understand what it is doing) :
import pygame
from pygame.locals import *
...
# Main game loop - this is the while loop you have in your code. This is where frames are rendered on every new loop
while True:
#Get all event that have been triggered from the last frame
events = pygame.event.get()
# Now we go through each event separately
for event in events:
#Use an if statement to check what the event is. For example...
if event.type == MOUSEBUTTONDOWN: #If this event was a mouse button click
print("Clicked") #Do whatever function
elif event.type == QUIT: #If this event was quitting the game, i.e: pressing the X button on the game window
pygame.quit() #Close the game
#and so on. You can have as many elifs to check all the events you need
game.fill((0,0,0)) #... The rest of your code goes after the for loop (event loop)
So essentially in your main game loop (your while loop) you have inside it a for loop (the event loop), then after it back in the main while loop you have the rest of the code you need to run on every frame such as rendering images and animating sprites.
A Better Way: Rect Objects
You will find that to see if the mouse click position (x, y) was in range of the mouse button, you had to check if the mouse click's x value was in range of the X1 to X2 of the object you're clicking (top right and top left corners), and at the same time it also has to be in range Y1 to Y2 of the object (top right and bottom right corners), which effectively means the mouse was somewhere on the object when the mouse click event was triggered
For this, Pygame implements Rect objects. A rect can be explained as an imaginary rectangle with a width and height that you define when you create it ( I will show you how that is done in code below ). To be more realistic, it is a pygame object for storing rectangular co-ordinates (a range of all points within that rectangle).
Now the thing is that this rect object has methods like Rect.collidepoint, which will return True if the mouse click position is inside the rect. This is a more simplified, easier to understand way of detecting if a mouse click position is inside a rect or outside it.
So..
# Constructing a rect at position (0, 0) with width 100 and height 500
r = pygame.Rect(0, 0, 100, 50)
# Now lets assume that above there is code where a mouse click is detect and the position is stored in `mousepos`, where `mousepos[0]` is the X and `mousepos[1]` is the Y
# All we have to do now is tell pygame to check if this position is inside our Rect object *r* or not
if r.collidepoint(mousepos):
print("You clicked inside an imaginary box")
else:
print("You clicked outside my imaginary box")
# Simple!!!
Much easier and more readable! (especially after you remove the comments you will be dumbfounded by the simplicity of this (collidepoint means inside the rect).
In reality, this is not how you usually use rects, although it gives you the basic idea of what they are. Rects are used in a more object-oriented method where the button is a child class of the Pygame.sprite.Sprite class. Then you can assign a rect object as its self.image.rect property and use it from there. NOTE: this is not very detailed :D .
Also, this is not the only use of rects. You can check if one rect is inside another. You can check if a group of objects is inside a rect. You can even with only one line of code check whether one object of a group is inside a certain rect, and if so, remove it!
To understand more about how to code pygame from start to finish (as the extra part of this answer talking about events and rects is very incomplete and only there as a starting point), I would suggest this excellent yet very simple and easy to understand book. And all you need is a basic understanding of python syntax.

How do I stop my character moving through objects with actionscript 3?

I am new to flash and want to make it so when my character hits an object, they won't go through it, but still maintains control after they have hit it. I want it to be a solid object from all 4 points (top, left, right, bottom) of the object. Here is what I have been experimenting with...
function hitsTheObject(e:Event)
{
if (myCharacter.hitTestObject(Ball_mc))
{
gravity = 0
hitObject = true
}
if (dIsDown == true && hitObject == true)
myCharacter.x -=10
}
The first if statement works, though the second one turns off the dIsDown button I have coded. Any thoughts?
Edit: Basically I want the character to hit an object and for it to block the character, as if it was a wall.
Lewis, you're best bet is point collision. Here's some links to help you think it through and start off: http://www.wildbunny.co.uk/blog/2011/12/14/how-to-make-a-2d-platform-game-part-2-collision-detection/ and http://www.anotherearlymorning.com/2009/07/pixel-perfect-collision-detection-in-actionscript-3/
Check out my collision engine, it supports continuous/bullet collisions: https://github.com/Murplyx/AAE---Axis-Aligned-Engine

as3 hittestobject not working, dont understand why

the collision not working i cant understand why, i put collision movieclips in the object and it doesnt seem to recognise one of the but does with the other, sorry for the confusing way of stating the problem if you play the game you will understand. im open to changing the way collision works too as long as it works ill be super happy
I'll try to explain. When You click "Down" or "Up" hero (box_MC) collide with both doors "Top_Door" and "Bottom_Door". Inside "bang" function at first checking collision with "Bottom_Door", so, hero always go down (.y += 100) and second condition (Top_Door) never will be true. How to fix this? Add variable var lastAction:String;. This variable will store last action: "up" or "down". Inside "down_MC_P" function initialize this variable by "down". Inside "up_MC_P" — "up". Next, replace the first condition to this if (box_MC.hitTestObject(cycle[i].Bottom_Door) && lastAction == "down") and second: if(box_MC.hitTestObject(cycle[i].Top_Door) && lastAction == "up"). That's all.

Sikuli interact with more than 1 matching object

Hello so for example I have 4 balls that I must right click on, but when I right click them, nothing about the balls changes, how can I make it so that sikuli right clicks each of the balls and doesn't just get stuck on one of them?
You can use a for loop to search for all the balls.
Then in the for loop you can define what to do with it once found.
For example (Sikuli with Python):
Image_Balls = ()
for x in findAll(Image_Balls):
click(x)
Between the () you need to place a picture of how a ball looks like.
In this example he will find every ball on the screen and click on it.
You can also add a try, catch/exception in the for loop.
Then he will find every ball, and try to right click.
If the try failed, he will execute the catch (exception in python).
And will continue to look for the next ball, until there are no anymore.
Image_Balls = ()
for x in findAll(Image_Balls):
try:
rightClick(x)
except:
pass