#๐Ÿ”’ Python Hitboxes

28 messages ยท Page 1 of 1 (latest)

strange hawk
#

Hi, I need help with Python. I'm not going to be using Pygame, and I'm working on a practice assignment for comp sci. The thing is, I'm stuck on hitboxes. I've looked it up and I've tried using the abs() method and infinite while loops. I need my hitboxes to recognize when they're within 50 pixels of another image/sprite, and it's not picking it up.
I have multiple functions in my lines of code to break down what's happening, and including this I've got my keybinding to "w" "a" "s" "d". This doesn't seem to be the problem though. Below I'll place my code from then on:

def up():
    jeff.seth(90)
    jeff.fd(20)

def down():
    jeff.seth(270)
    jeff.fd(20)

def right():
    jeff.seth(0)
    jeff.fd(20)

def left():
    jeff.seth(180)
    jeff.fd(20)

bob = Turtle()

a = Turtle("
badsnake.gif")
a.pu()
a.speed(0)
a.goto(random.randint(-400,400), random.randint(-400,400))

jeff = Turtle("capybara.gif")
jeff.color("blue")
jeff.speed(2)
jeff.pu()
print(jeff.pos())

a_hitbox= Turtle()
a_hitbox.color("red")
a_hitbox.pensize(5)
a_hitbox.speed(0)
a_hitbox.pu()
a_hitbox.goto(a.xcor()-30, a.ycor()-30)
a_hitbox.seth(90)

def border():
    
    bob.color("red")
    bob.pensize(5)
    bob.speed(0)
    bob.pu()
    bob.goto(jeff.xcor()-45, jeff.ycor()-45)
    a_hitbox.pd()
    for i in range(4):
        bob.forward(90)
        a_hitbox.fd(60)
        bob.left(90)
        a_hitbox.right(90)
    #bob.hideturtle() #makes turtle shape disappear

window.onkey(up, "w")
window.onkey(down, "s")
window.onkey(right, "d")
window.onkey(left, "a")

while True: #declares an infinite loop
    border()
    if bob.pos() == a_hitbox.pos():
        print("Collision")
    
    window.listen()
blazing zenithBOT
#

@strange hawk

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

polar knoll
#

!code

blazing zenithBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

polar knoll
#

could you format the code like this please

strange hawk
#

Is it this one? `

keen swallow
#

yes

strange hawk
#

I did it

keen swallow
#

3 backticks before py, return, code, return, 3 backticks

polar knoll
#

the whole code block

#
like this

print("test")
strange hawk
#

oh

#

ok!

#

I did it again!

keen swallow
#

from turtle import Turtle

def up():
    jeff.seth(90)
    jeff.fd(20)

def down():
    jeff.seth(270)
    jeff.fd(20)

def right():
    jeff.seth(0)
    jeff.fd(20)

def left():
    jeff.seth(180)
    jeff.fd(20)

bob = Turtle()

a = Turtle("
badsnake.gif")
a.pu()
a.speed(0)
a.goto(random.randint(-400,400), random.randint(-400,400))

jeff = Turtle("capybara.gif")
jeff.color("blue")
jeff.speed(2)
jeff.pu()
print(jeff.pos())

a_hitbox= Turtle()
a_hitbox.color("red")
a_hitbox.pensize(5)
a_hitbox.speed(0)
a_hitbox.pu()
a_hitbox.goto(a.xcor()-30, a.ycor()-30)
a_hitbox.seth(90)

def border():
    
    bob.color("red")
    bob.pensize(5)
    bob.speed(0)
    bob.pu()
    bob.goto(jeff.xcor()-45, jeff.ycor()-45)
    a_hitbox.pd()
    for i in range(4):
        bob.forward(90)
        a_hitbox.fd(60)
        bob.left(90)
        a_hitbox.right(90)
    #bob.hideturtle() #makes turtle shape disappear

window.onkey(up, `w`)
window.onkey(down, `s`)
window.onkey(right, `d`)
window.onkey(left, "a")

while True: #declares an infinite loop
    border()
    if bob.pos() == a_hitbox.pos():
        print("Collision")
    
    window.listen()

should look like this, I just guessed at your import though, would be better if you did it

strange hawk
#

I'm sorry

keen swallow
#

Not trying to make you feel bad, it's just so we can help

strange hawk
keen swallow
#

that's unfortunate...

strange hawk
#

Does it still work

keen swallow
#

I had AI fix it up, is this what you're seeing?

strange hawk
#

No, I can add in the images if you'd like them to be in your files

keen swallow
#

oh right, that's not really helpful and I could do more harm than good if we're on different pages, it would be best if you could give us your code

strange hawk
#

That is my code

#

This is what I see when I run the code, it's just I need the actual hitboxes to recognize each other within a certain amount of pixels

keen swallow
#

Take a look at the Turtle docs, I need to take the dog out but look at the turtle.distance() method. normal collision logic will routinely check this

blazing zenithBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.