Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Scene, Game Collision & Gravity Issues
-
@xvid when I try to run it, error AttributeError: 'Player' object has no attribute 'collision_mix' in
self.player.collision_mix
-
@cvp ups sorry, have edited now it should be working with the given code :)
If there are still some errors, hopefully not, let me know! -
@xvid no errors, but I commented some prints, which, I think, crash the program because too they happen too often.
But, I don't understand the game, surely I'm too old for it 😥👴🏻 -
@cvp for real? 🤔 I commented/removed all prints, it should work in this state as no function rely on a print statement. If you have a look would be great maybe you have more experience :)
BTW it's not a "game" in this current state, just an environment to check if the collision and controls are working.
-
@xvid I didn't see the shoot button on my iPad mini4.
You should set your x,y in function of SCREEN_W and SCREEN_H,
like 0.9*SCREEN_W -
I don't know what to do. As soon as I touch the screen, the alien falls and I don't know how to let it jump up. Sorry, but I'm not used to this kind of game...
-
My fault. I had incorporated the level.y into the script and forgotten to split it into lines...
I didlines = level#.level.splitlines()
instead of
lines = level.splitlines()
-
@cvp ah I see! Yeah I'm using an iPad Pro 11". It could be maybe not compatible in this case with other devices. There should be a "Left", "Right", "Jump" and "Shoot" key on the device screen.
-
@xvid I had Understood, I had put the shoot at x=900 because 1100 is not visible for me
-
If I correctly understand, your first problem is that your alien stays so flying
-
@cvp Yes correct, as soon as the alien touch the ground
self.onGround = True
but will not change anymore expect you press jump. -
@xvid is it not due to velocity.y which is 0?
-
Try
def collision_up_down(self, player_bbox, walls): for w in walls: if self.bbox.intersects(w.bbox): if self.velocity.y > 0: new_y = w.bbox.min_y - self.bbox.h / 2 self.position = (self.position.x, new_y) self.onGround = False print("collide top", self.onGround) if self.velocity.y < 0: new_y = w.bbox.max_y + self.bbox.h / 2 self.position = (self.position.x, new_y) self.onGround = True self.velocity.y = 0 print("collide down", self.onGround) return new_y = self.position.y + self.velocity.y if new_y > 0: self.onGround = False
See the return if intersects, you don't need to continue the loop because I suppose you can't intersect more than one wall
-
You can put the same return if intersects in the
collision_left_right
-
@cvp Thank you very much! Yeah it could be also because
velocity.y
is 0. Your code seem to get the gravity problem fixed but now thecollision_left_right
isn't working probably. You see the alien is teleported to the top of the tile if I run against it from left/rigth.Here is a clip with what I mean: GIF
-
The second problem comes from that if you intercepts with a wall in collision_left_right, you will also intersects in collision_up_down and there you will set the y at up side of the wall
-
Ah you were faster haha thanks!
Yes exactly, so I kinda need a function which distinguish which collision should be triggered, right? -
@xvid I think so
-
You need to figure out which edge of the wall is hit. Or, you should back the player up by -velocity until it clears the hit. Your code always moved to the top or left or bottom of a tile, which only works if velocity is totally horizontal or vertical. So, you need to move only in the direction of the touched edge(careful if more than one edge is touched ), or backwards along velocity might be better.
Better yet-- check for future collisions when moving the player, not after he moved.
Or, overlap of an edge is treated like a really high impulse of acceleration along the direction perpendicular to the edge, which will give you bounce. Though you'd want to also implement friction/damping of some kind.
-
@JonB I already feared I have to do something like this. I assume the method where I check for future collision might be the best but not the easiest one, right?
I probably have to check out some more advanced tutorials for this 🙃.