This question already has answers here:
How do I detect collision in pygame?
(5 answers)
How to detect collisions between two rectangular objects or images in pygame
(1 answer)
Closed 2 years ago.
I'm using Pygame with my group to develop a game for this semester in school. I'm responsible for building the physics engine.
My question: How do I get a player object to stop on collision with a rect object? I've scoured the internet, and this website for answers, but everything seems to complicated to grasp.
Here's the code particular block of code I'm trying to get to work:
if player.rect.left == rect.right:
player.undo_right()
This block is preceded by the game loop. All I want to do is make it so the player is consistently 20 pixels away from the box. I'll be able to apply better bounds once I get this test to work. Right now, it repels the player sporadically, and never consistently. Sometimes, I'll run it, and it'll bounce away once, and then the player will go through the rectangle freely.
I realize there are numerous links on the internet for this type of thing, but I wanted to try to get collision to work using simple code like this. If I'm not able to, then why? Can someone exaplin a simple and efficient way to do this?
Thanks.
Related
This question already has answers here:
how to make image/images disappear in pygame?
(1 answer)
How do I delete rect object from screen once player collides with it?
(1 answer)
Closed 1 year ago.
I am making a game in python using pygame for the visual interface and pymunk to simulate the physical collisions between the ennemies and everything works just fine but now i want to remove the enemy bodies when they die (get to 0 health), so i used pymunk.Space.remove(space, enemy.shape) and pymunk.Space.remove(space, enemy.body) and so far everything works , when the enemy dies he disappears but the problem is once the image disapeared the pymunk "bodies" that process collision still persist which then blocks the other enemies. Also i dont wan't to just once they die move their bodies out of the map as i'm worried about it causing lags > see the photos
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
This question already has answers here:
How to get keyboard input in pygame?
(11 answers)
How can I make a sprite move when key is held down
(6 answers)
Closed 2 years ago.
So I have a small problem.
I am a beginner and I am just making my first game, chose to do a pong clone.
However, I can not move the second paddle with the keys a and z and for the life of me I canĀ“t find out why.
I would be extremely grateful if someone could help me.
Just for heads up, laud mean paddle, pall means ball and aken means screen. I am not a native speaker.
This question already has answers here:
Why does my game made with Pygame suddenly lag for a few seconds? [duplicate]
(1 answer)
Pygame is running slow
(1 answer)
Closed 2 years ago.
I'm designing a game in pygame that you can download here: https://github.com/hailfire006/games unfortunately, when you fly the ship near the space station, the framerate drops from a nice 30-40 to 16-20. I believe this is due to my blitting sprites onto the screen every frame, and I have been told that there is a way to only redraw parts of the screen that need redrawing, but no one ever explains how to do this.
Someone please help, I'm beginning to worry that I should abandon hope of ever making a good game in python, and that I'll have to learn an entirely new language just to deal with framerate...
p.s. any other way you know how to raise the fps would be welcome also
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.