working sprite collision [duplicate] - sprite

This question already has answers here:
How do I detect collision in pygame?
(5 answers)
Closed last month.
I am having two problems with my code:
I don't know how to make my character check that it's colliding with the blocks
How do I blit to a new screen every time she completes a level? So when the character reaches the stair then a new screen should appear showing the new level
Here is all my code :
http://pastebin.com/u/bluesplay106
I am pretty new to pygame so my style may not be good and I kind of hard coded this game.
If you could tell me how to fix my problem that would be really great!!

for the collision detection you need to make your character and your blocks into sprites and do a collision detection that way.
As for the new screen when you get to the stairs, you can use a statement like this:
if heroSprite_x >= 200 and heroSprite_x <= 300:
if heroSprite_y >= 300 and heroSprite_y <= 400:
#go to new screen using either a new level from a list or a new class or whatever method you want.
Your question was a bit vague but I hope that's what you were looking for :)
I just realized I didnt answer your first question, here's a snippet from one of my games:
unit_enemy = pygame.sprite.groupcollide(unitgroup, enemygroup, True, pygame.sprite.collide_mask)
for hit in unit_enemy:
#do something
This checks if any enemies hit my unit. So in you case it would be your hero, and the enemies would be blocks.
Here's the documentation on collision detection.
http://pygame.org/docs/ref/sprite.html

Related

Godot debug window not triggering area2d signals

Video of what's happening exactly
Specs are:
Display being recorded 2560x1080#60hz
Display that the window goes offscreen 3840x2160#24hz (tested with 60hz too)
Windows 10
GPU Nvidia 970 GTX
Just started learning godot this week and lost hours to this strange behavior.
Godot specifics:
Scale of shapes and bodies are not modified (not to mess with physics)
Starting out trying to create an Entity class, that extends KinematicBody2D, to create instances of enemies within my game. Just using the dummy block for now to test if collisions are indeed working (stopped here due to what happened on the video)
The big dummy square that has one square texture is said Entity, with a collision with huge Y size just to test things out.
The area2d I want to trigger the signal is the small rectangle in front of the character.
Is there something I should know that is causing the signal to only fire while the debug window is on the other display? Should I just move the debug window to the other display and trust the game will work?
Code snippets
Creation of Area2D inside my animation controller
func _create_shape_with_collision(s : Shape2D, parentNode : Node) -> Node:
var ret = Area2D.new()
ret.connect('body_entered', self, '_check_body_entered')
var c = CollisionShape2D.new()
c.shape = s
parentNode.add_child(ret)
c.disabled = true
c.name = 'collision_shape'
ret.add_child(c)
return ret
signal function
func _check_body_entered(body : Node):
print(body.name)
PS: Attempted to type some stuff to help with autocomplete within Godot's interface, but my created classes were not working properly.
PS2: posted this same message on godot's reddit page, in hopes of better visibility
Make sure you have a matching Collision layer and mask in the Area2D node.
Shape2D is a resource, meaning if you don't create .new() then all CollisionShape2D are using the same instance of the shape. If anyone sets explicitly it free it's gone for everyone (so try printing if you accidentally are assigning a null shape). There's some weirdness on resizing after assigning not happening when CollisionShape2D is ready.
But also you have c.disabled = true that probably shouldn't be there.
Because #Theraot laid a path towards writing an issue on Godot's Github page, I was able to determine the cause of the above bug, while creating a minimal project.
In fact, it does not relate at all with the code, but on project settings. The physics FPS was set at 60, while my secondary screen was able to capture the event, as VSYNC was also on by default (caps FPS at refresh rate, secondary screen is 24hz).
Increasing the physics FPS to 120 (empyrical at this point) made the signal work on all displays.
Will gladly accept a better answer, explaining what are the impacts of changing such number (the strain on devices etc), if it is a bad practice or if there is another way to configure individual faster ticks for Area2Ds or PhysicsBody2Ds. Could not write such answer due to my current lack of knowledge/research. Trying to write it at this point would be a sophism.
Since i'm just coding some basic game elements while writing a Game Design Document, it's so far so good for me.
Edit:
After an insight from user #Theraot, I overlooked how different _process and _physics_process worked; For my use case, most of what I was doing should be inside _physics_process, so I could revert the engine's physics FPS back to default value.

Check if movement ended

When I start a matter.sprite movement in Phaser 3 with sprite.setVelocity(vx, vy), how can I easily check if the movement ended?
The problem is that sprite.body.velocity.x == 0 seems to be only true in the case when the sprite bounces off of an obstacle, and momentarily stops. But I need to know when the whole movement actually ended.
Well, this was a problem on my side. I was testing a maximum velocity of all objects to check if there is a movement on the scene. But I was doing it like this:
maxVelocity.x = Math.max(maxVelocity.x, child.body.velocity.x);
Which doesn't work well with negative values. Math.abs(...) was needed :)

Flip horizontally and vertically single frames of an animated sprite

I have 4 (0-3 animation frames) different images of a coin animation with differet states of it spinning. I would like to make it look like its spinning by adding another 2 frames (4-5) to make it look like it spins. In current situation it looks like the coin spins 180 degrees and goes back to it's original position. I would like to flip vertically ONLY the 4th and 5th frame. How can i achieve that without making new redundant pngs?
I know making 2 new pngs is not a big deal in this case, but if i had more frames, and/or bigger sprites it could make a significant difference in future projects.
I was thinking you could make the animation flip horizontally when it reaches the last fame, however that would mean you would have to omit #4 and #5 to avoid duplicates, but it still would look odd because frame #3 would skip directly back to frame #0. I tried thinking of different orders the sprite frames could be placed in to clean this up, but all my solutions either result in duplicate frames, or ugly skips. Maybe you might be able to solve what I couldn't.
Unfortunately, it seems that though Godot supports play_backwards functionality for the animation_player node, there is no such feature when it comes to animated_sprite. Too bad, because this would likely solve your problem.
For now, I think your best bet is to do what you were trying to avoid.. flip the sprite images yourself and add them as animation frames, or make a separate animation containing the flipped sprites and have them switch back and forth after their last frames, respectively. It might be a lot of work for super large projects, but without some kind of play_backwards functionality for animated sprites, I don't think it's worth breaking your back trying to find a workaround this time.
Of course, if I am wrong, I would welcome any correction from the community.
Good luck.
this is one way to accomplish the task.
Inside the ready function of godot write
#used to store frame value
var frame = 0
Inside the function that your using that runs every frame write:
frame += 1
#resets the value representing the current frame
if frame > 5:
frame = 0
if frame == 4 || frame == 5:
#this should be whatever function you use to flip the sprite vetically
flip_h = true
else:
#this should be whatever function you se to flip the sprite horizontally
flip_h = false
You probably already know this because I recognize the udemy course this screenshot is from, as I am doing it right now, and this is explained later in the course (when adding spikeman enemy). But for those who don't know it can be solved by adding an AnimationPlayer and adding new animation track that deals with animation flip_h property of AnimatedSprite. Then you have to set flip_h on those last 2 duplicated frames.
You could use the signals to detect when the animation has finished

Making an enemy AI that follows a set path

So I'm making a game for a CS project at school that is based on the game alien isolation (but top down 2D) and I am not sure where to start with the alien's methods of hunting the player. I would quite like to make a neural network however I'm not sure how to make one specific to my problem (e.g. the data given to the network, etc).
I would like the alien to follow a set path that is drawn on the map and essentially be able to intelligently follow/hunt the player without getting stuck at a dead end on the path for example.
I've already tried messing around with a simple algorithm which based the desired direction on x and y positions of the last known position of the player. But this lead to the alien getting stuck and being generally unintelligent. There's not any code to look at really because I need to completely rethink this, but it might be useful seeing t=how the alien checks whats paths are available, just for context.
def navigate(self, player, window):
available = []
nodes = [node_pos1, node_pos2, node_pos3, node_pos4]
//nodes are the points just around the center of the alien which..
//..detect the colour to check for available paths
for node_no in range(len(nodes)):
// checks the colour at each node
if Graphics.get_at(background, nodes[node_no]) == (0, 255, 0, 255):
available.append(self.direction_index[node_no])
if not available: // if no paths, it will find the nearest path
self.find_path()
self.sight(player, window) // looks for player
speed = self.speeds[self.status] // sets the speed
self.status(available, speed) // runs the move type
I understand this is quite a big ask but I'm looking for some pointers of how to start a neural network for this problem at the very least.

Key and lock code [game programming]?

Metaphorically speaking, I'm learning python in a community college for game programming; and our second assignment is to make a text based game. I'm stuck trying to figure out how to get the code to run if the player has something in their inventory, then display these options or print these options if they don't have that certain item in their inventory.
What you describe is what was a very popular type of game a long time ago. The most difficult portion of this type of game is interpreting user input. You can start with a list of possible commands, iterating through each token of the user's input. If I type
look left
the game can begin by calling a method or function which interprets the available visual data. You will want to look into your database implementation for what is left in relation to the current player's position. The game can respond with
You see a wooden box on the floor. It is locked.
You get the idea. As the user, I will probably want to check my inventory for a key. You can implement a subroutine in your parser for searching, categorizing, and using your inventory. The key to writing anything like the described program is spending at least twice as much time testing and debugging the game over how much time you spent coding the first working version. The second key is to make sure you are thinking ahead. Example: "How can I modularize the navigation subroutine so that maps can be modified or expanded easily?"
Comment if you have any questions. Good luck coding!

Resources