this is a small part of my code where i created the collision but the issue is that it still doesnt work i have used also the distance formula to figure out the collision still nothing showed up;
please look at the screenshot and help!

Recommended Answers

All 18 Replies

Capture.PNG

Capture2.PNG

This post has no text-based content.

I'd add a print statment to see exactly what is happening. If you are using a debugger with breakpoints then also add one at the start of your dead function.

collision = isCollision(fighterX[i], fighterY[i], playerX, playerY)
if collision:
    dead()
collision = isCollision(fighterX[i], fighterY[i], bulletX, bulletY)
if collision:
    print("shot")

def isCollision(fighterX, fighterY, playerX, playerY):
    distance = math.sqrt(math.pow(fighterX - playerX, 2) + math.pow(fighterY - playerY, 2))
    print("distance=", distance)
    if distance < 27:
        return True
    else:
        return False

I don't know if you need to keep the value of collision for later but your code could also be written

if isCollision(fighterX[i], fighterY[i], playerX, playerY)
    dead()

if isCollision(fighterX[i], fighterY[i], bulletX, bulletY)
    print("shot")

def isCollision(fighterX, fighterY, playerX, playerY):
    distance = math.sqrt(math.pow(fighterX - playerX, 2) + (math.pow(fighterY - playerY, 2)))
    print("distance=", distance)
    return distance < 27:

I might also suggest you change the naming of the parameters to make them more generic such as

def isCollision(object1X, object1Y, object2X, object2Y):
    """Return True if object1 and object2 are closer than 27 units"""
    distance = math.sqrt(math.pow(object1X - object2X, 2) + math.pow(object1Y - object2Y, 2))
    print("distance=", distance)
    return distance < 27:

If you are running the latest (3.8x) version of Python they have added a self documenting print formatter which is used like

print(f'{distance=}')

If, for example, distance is 43.9 then the output is

distance=43.9

Also note that I removed a redundant pair of parentheses in your distance calculation.

commented: Science. +1 +15

thanks u haven't just help me but showed different of doing that, Thanks

the code u showed me only calcualates distance but collision doesn't happen

the format that i asked might not be understandable but what i meant was that to find the distance between the bullet and the enemy and to fire the bullet if the enemy and player distance is less than 27 u can't fire any bullet.sorry for confusion

collision of the player and enemy worked but for the bullet not yet.

commented: Could be a size issue. Make the bullet huge as a test. +15

sorry to post this but i had to

the screenshot show how i defined the bullet function and when pressed it releases the bullet for releasing bullet is working but it is not colliding.

the code u showed me only calcualates distance but collision doesn't happen

It returns True if the objects are closer than 27 units apart. I presume your object positions are defined by a centre point. In that case you should probably pass the object widths (or radii) and use those numbers to determine dynamically what constitutes a collision. For example, assuming round objects for simplicity, if object a has a radius of 23 and object b has a radius of 9 then they are touching if they are coser than 23+9 or 32 units apart. I also assume a bullet has a much smaller radius than a target.

the format that i asked might not be understandable but what i meant was that to find the distance between the bullet and the enemy and to fire the bullet if the enemy and player distance is less than 27 u can't fire any bullet.sorry for confusion

I didn't understand that but in order to make the code more general and perhaps clearer you might want to create a distance method that does nothing but return the distance between two objects. Then your code looks something like

if distance(objecta, objectb) < mindistance:

You could also define your objects as classes (actual objects) and use notation like

fighter1.pos
fighter1.pos.x
fighter1.pos.y

or just

fighter1.x
fighter1.y

instead of polluting your namespace.

thank you that helped so much.
can you please explain to me how to create a different file which is menu or introduction then after i have clicked play it should go to the game file.but i dont know how i can make it do i have to import filename.

for example i have create menu file and game file differently i would like after iam done with menu to go directly to the game loop but when i separate the file iam sure it will display not defined, what should i do
Thanks,

I don't understand. File, menu, and introduction are (up to) three distinct things. If you just want a text based introduction you could always put it in a python module as a triple-quoted (multi-line) string like

intro = """
It returns True if the objects are closer than 27 units apart. I presume your object positions are defined by a centre point. In that case you should probably pass the object widths (or radii) and use those numbers to determine dynamically what constitutes a collision. For example, assuming round objects for simplicity, if object a has a radius of 23 and object b has a radius of 9 then they are touching if they are coser than 23+9 or 32 units apart. I also assume a bullet has a much smaller radius than a target.
"""

instead of putting that text into a file and then reading it in.

from Settings import * thats is what i was asking about to create a different file and import it in a different file.

the reason is that when i put it inside the game file when the game start it wont show up and i wanted it to appear first when i run my code then after play button is clicked to go to the game function.

thank you for the help the problem is solved

iam sorry for my distrubance could u please help me here
i want the score_value to increase to 10 points but its not increasing at all what might be the issue and how to solve it

from Settings import * thats is what i was asking about to create a different file and import it in a different file.

I've never gotten into the finer points of setting up modules for import so I can't explain how they work. However, what I did for my own use was set up a folder, D:\include. I then set the Windows environment variable, PYTHONPATH to D:\include. When I want to include a file (like autoit.py) from my include library I do

from autoit import *        # from include

The comment is so I can do a grep to check what scripts reference my include folder.

If you don't want to use PYTHONPATH you can add the folder at run time like

import sys
sys.path.append('d:\include')

its not increasing at all

Is that block of code being executed? i.e. is collision==True? If noth then you'll have to debug the isCollision method.

While I'm thinking about it, when you post code, could you please post the code as text using the code insertion </> tool? That way it is easier if someone (like me) wants to copy the code into a local file to test. It makes it easier for us to help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.