I wrote a program using all pygame modules. Some of it looks like this:
#assigning keys to the key press library
keys = pygame.key.get_pressed()
#Key movements
if keys[pygame.K_w] or keys[pygame.K_UP]:
c_one_y -= speed
if keys[pygame.K_s] or keys[pygame.K_DOWN]:
c_one_y += speed
if keys[pygame.K_a] or keys[pygame.K_LEFT]:
c_one_x -= speed
if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
c_one_x += speed
screen.fill(maincolour)
pygame.draw.rect(screen, (225, 0, 0), (c_one_x, c_one_y, width, length))
pygame.display.update
The aim is to move a rectangle in any direction using wasd's and the arrow keys. My IDE isn't reporting any errors yet the rectangle isn't appearing where it is set to. I have tried various different approaches to the problem but none have worked. Got any suggestions?
It looks like you haven't made pygame.display.update() a function. You have just written pygame.display.update.
Related
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
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 )
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.
I implemented the CCD algorithm for Inverse kinematics, it works great but it fails with constraints, I want to implement a system in which if the arm cannot reach the target it tries to get closer to it.
I tried to put the constraints in CCD algorithm, that is if my rotation angle goes above or below the constraint i limit it to max or min. for example, if the rotation angle is 100 degree and the constraint is 90, then I rotate 90 and based on that I calculate other angles, It works for some cases but fails for most.
CAn anyone tell me a 3D IK algorithm which takes care of constraints?
CCD itself will work like a charm.
If you are working on 3D, first find the rotation you should do in each axis without applying bone limits.
After that, the approach
expectedRotation.X = min(expectedRotation.X, maxLimit.X)
expectedRotation.X = max(expectedRotation.X, minLimit.X)
expectedRotation.Y = min(expectedRotation.Y, maxLimit.Y)
expectedRotation.Y = max(expectedRotation.Y, minLimit.Y)
expectedRotation.Z = min(expectedRotation.Z, maxLimit.Z)
expectedRotation.Z = max(expectedRotation.Z, minLimit.Z)
is wrong. Because, if you can't move in one of the axis further, the other two axis will keep moving and you will get weird results.
The fix:
If any of the 3 axis mismatches with the limit constraints, you must not change the rotation at all.
First convert all the angles into degrees in -180 to 180 format. Then the following will do
vector3df angleDifference = expectedRotation - baseRotation; //baseRotation is just the initial rotation from which the bone limits are calculated.
if(angleDifference.X < boneLimits.minRotation.X || angleDifference.Y < boneLimits.minRotation.Y || angleDifference.Z < boneLimits.minRotation.Z || angleDifference.X > boneLimits.maxRotation.X || angleDifference.Y > boneLimits.maxRotation.Y || angleDifference.Z > boneLimits.maxRotation.Z)
return currentRotation;
return expectedRotation;
I have a pygame.Timer running in my game calling a draw function 32 times/second. The drawing method gets positions from all elements on my screen and blits them accordingly. However, I want the main character to walk around slower than other objects move.
Should I set up a timer specifically for it or should I just blit the same frames several times? Is there any better way to do it? A push in the right direction would be awesome :)
(If anyone's interested, here is the code that currently controls what frames to send to the drawing: http://github.com/kallepersson/subterranean-ng/blob/master/Player.py#L88)
Your walk cycle frame (like all motion) should be a function of absolute time, not of frame count. e.g.:
def walk_frame(millis, frames_per_second, framecount, start_millis=0):
millis_per_frame = 1000 / frames_per_second
elapsed_millis = millis - start_millis
total_frames = elapsed_millis / millis_per_frame
return total_frames % framecount