pygame sprite tagging and analyzing the stats of several sprites and one sprite

Question:

I tried to portrait my progress chart for my pygame test(simple test for making sure what function I should use) project, here is the chart:

1. call Player:

Make player into all_sprite(), players() group,

and shoot() the skill every second.

2. call Enemy:

Make Enemy into all_sprite(), mobs() group,

and gradually approach to the player.

3. call Skill:

shoot():

Make skill into all_sprite(), skills()

the skill make per second will link to the Enemy that is the nearest to the player

and maybe tag these two or someway else to make sure they will finally collide

4. attack:

When the Skill gets linked to the Enemy, the skill will apporach the moving Enemy with same speed.

enter image description here

This is the brief progress chart I designed for the fucntion I want to display, however, there must be several Enemiese exist on the screen while the porgram is functioning, how can I analyze all the Enemies’ position everytime the Player shoots the Skill out?

I use the code

for i in range(8):
    enemy = Enemy()
    all_sprites.add(enemy)
    mob.add(enemy)

to call out 8 Enemies at the same time from different dicrection, how can I check each one’s position and tag or make it have number? For example, for Player at O( 0, 0), EnemyA at A( 10, 15), EnemyB at B(20, 15), EnemyC at ( 3, 4), when Skill1 get shot out it get linked to the EnemyC while it’s the nearest to the Player.

or should I use different way to call the Enemies?
The point is that how can I get the list of the sprite I created at the same time?

Because I used the for loop to call out 8 sprite in same class, does the system consider them as the same element, or is there a list that can show the number of the sprite indivisually?
Like[sprite_1(x1,y1), sprite_2(x2,y2),…,sprite_n(xn,yn)]

Also how should I make the skill that is tagged approach to the Enemies it got linked to ?

Asked By: TeriChang

||

Answers:

can use trigonometric here

import math

def measure_distance(x1, y1, x2, y2):
    dx = abs(x2-x1)
    dy = abs(y2-y1)
    distance = math.sqrt((dx*dx)+(dy*dy))
    return distance

then compare the distance between A-O and B-O

Answered By: danangjoyoo
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.