I am relatively new to Pygame and trying to make a top-down racing game. Everything has currently been made and is ready to go! However, I am unable to make proper collision detection between walls and background of the "level".
What I want to happen is that the car cannot leave the boundaries of the track it is pushed back onto the track and speed is reduced. Previously this was attempted with pygame.sprite.collide_rect to no luck.
Here is a screenshot of the first track within the game and one of the cars in-game. A background will be added at a later date.
Track Screenshot
Any advice on which functions to use would be much appreciated! Would be possible to even use .collidepoint like done in menus?
Thanks,
Adam.
What you could do is, check if the two images overlap, but one condition: the image of the track must be full opacity, and everything else transparent. Then use this code to check if overlapping.
Car_mask = pygame.mask.from_surface(CarImage)
Track_mask = pygame.mask.from_surface(TrackImage)
offset_x, offset_y = (Car.x - Track.y), (Car.y - Track.y)
if (Track_mask.overlap(Car_mask, (offset_x,offset_y)) != None):
print("overlaps")
else:
#Push Car Back On Track, Slow Speed Stuff
#Cause it not touching track at all.
Related
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
As part of my current pygame project (I am very new to pygame), I have created multiple rectangles that move at random around the screen. As one of the features of this game, I want to make it so if one rectangle is close enough to another one, it moves towards it. Another feature I want to add is that rectangles cannot collide with each other, but I don't want to just do the regular
if rectangle1.colliderect(rectangle2):
rectange.x -= 10 # That is, it's previous position
because it will make the animations look odd.
The main way I can see to solve these problems is to use some sort of function that could check if a rectangle.x - 30 is another rectangle (or something similar), but I am unaware of one that exists.
I have attempted to look through google, but I haven't found anything as all the posts are different problems that aren't quite the same.
Thank you for any responses!
Use inflate to create a rectangle that is larger than the original one and surrounds it. Use this rectangle to find other rectangles in range:
test_rect = rect1.inflate(dist, dist)
if test_rect.colliderect(rect2):
# [...]
So basically, I'm going to make a Brick Breaker type of game. Just my beginning CS Python class didn't teach much OO programming, and I was wondering how I could make this free moving ball register when it hits the slider. I think I have an idea, but I would like to see other peoples explanations.
You know the position of the two objects that can collide and compute the distance. When this is smaller than a threshold then they collide
You use the Canvas.find_overlapping(*rectangle). to find out the figures on the canvas in a rectangle.
I always prefer option 1. It helps dividing model and presentation to the user which do not always need to be linked.
I am making a tile based game, and the map needs to be rendered every frame. Right now, each tile is 32X32, and the visible map is 28X28 tiles. The performance is dreadful. I recently made it only render the visible tiles, but this still did not improve the FPS much. Right now I'm looking for a way to speed up the rendering. I attribute the slowness to the way I am rendering ; every tile is individually blitted to the screen. What would be a more effective was of doing this?
In pygame (afaik), updating the screen is always one hell of a bottle neck. Since I could not see your code, I don't know, how you are updating the screen. Only blitting the the sprites that changed is a start, but you need to only update those parts that changed, on the screen.
Basically it is the difference between using display.flip() or using update_rects() with only the changed rects. I know, that does not help at all, when you are scrolling the map.
Take a look at this question: Why is this small (155 lines-long) Pacman game on Python running so slow?, it has a similiar topic.
One thing I tried when I had a map compiled of tiles and some sprites on it, I tried always having a precompiled image of the map for an area containing the currently displayed part and some 200 or so pixels around that, so that I could blit the prepared "ground" (still only in updated parts) without the need of blitting all those tiles contained in it. That, of course, is quite some thinking you have to put into that, espacially if you have multiple layers and parts of the map that can be above your active sprites. It is interesting to think and work that through, but I cannot tell you, how much you will gain by that.
One totally different possible solution: I began with pygame once (since I did SDL in C++ prior to that). Recently I was directed to another python gaming library: pyglet. This does not suffer from the problems of updating the whole screen as much as pygame (I think it's because of usage of OpenGL acceleration; it still works on my not at all accelerated eee-Netbook). If you are not bound to pygame in any way, it might be interesting to take a look at pyglet.
I would like to create a list of objects that collide with the user. However, I don't want to use the sprite.collide_rect_ratio() method because it creates a rectangular area that is too big for the collision (i.e. the objects seem to collide even though they are not really touching). I want to use the pygame.sprite.collide_rect_ratio(ratio): to fix the problem. How do I implement the method so that it returns a list of objects the user collides with?
It would implement the same code except with a smaller collision area as the following code:
sprite_list = pygame.sprite.spritecollide(myself, all_sprites_list, False)
Thank you.
This may be of use to you,
What you may want to look into is "Per Pixel Collision", which will first use the bounding box of the object (what i suspect the collide_rect function does).
What you will need to do is find where the rectangles collide and how far within each other they are. You then check to see if there are any opaque pixels from one sprite touching any opaque pixels from the other sprite...
This Link may be of use to you, its a very well done tutorial for a C++ framework similar to pygame.
The Per Pixel Collision code is half way down the page, and acts how I describe above.
Hopefully this is useful to you as it negates the need for the rectangle ratios due to 'invisible collisions'.
a quick google search may help you more with this type of collision detection.
For a bit of a boost heres some sample pygame code:
for s in sprites:
# if no intersection then 'intersection' will be of size 0
intersection = s.Rect.clip(user.rect)
if intersection.width != 0 and intersection.height != 0:
# perform collision detection
Here is an already written and tested version From the pygame wiki. Reading every thing on that page will give you a good knowledge on pixel collision and some good sample code which you can use straight away.
apologies if this was too far off topic but I feel this could be very useful to you as ratios (i feel) would not perform well for collision detection.
You may also want to look at Rectangle documentation in pygame.
As for your question, looping through all sprites and using the collide_rect_ratio method would be the only way of using such a method to get a list of colliding sprites as far as i know.