I got this attribute error when rendering my font

from turtle import distance, pos
import pygame
import math

dx = 3     # Speed in X direction
dy = 4     # Speed in Y direction
x = 100    # X position
y = 100    # Y position
radius = 20
pygame.init()
clock = pygame.time.Clock()
display = pygame.display.set_mode((500, 300), pygame.SRCALPHA, 32)
while True:
    clock.tick(30)                   # Make sure 1/30 second has passed
    for event in pygame.event.get():
        mouseX = pygame.mouse.get_pos(0)
        mouseY = pygame.mouse.get_pos(1)
        if event.type == pygame.MOUSEBUTTONDOWN:
            if distance((mouseX,mouseY), (x, y)) <= radius:
                x = 100  # X position
                y = 100  # Y position

    display.fill((100, 100, 100))    # Clear the screen
    x = x + dx                       # Move objects
    y = y + dy
    pygame.draw.circle (display, (200,200,200), (x,y), radius) # Draw the ball
    if (x< radius or x>500- radius): # Outside of the screen in x?
        dx = -dx                     # Change the motion direction in x
    if (y< radius) or (y>300- radius):  # Outside of the screen in y?
        dy = -dy                     # Change the motion direction in x


    pygame.display.update()          # Update the screen

what the does this even mean : its showing error in line 263 i dont even have a line 263 what am i supposed to even do please help its also showing error in line 1859 whaaaaaat? i dont even have 100 lines of code its just 34 lines this is the error : Traceback (most recent call last): File "d:\Code\CompanionFiles.GameDev\Code\Chapter 3\game01.py", line 20, in <module> if distance((mouseX,mouseY), (x, y)) <= radius: File "<string>", line 8, in distance File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 1859, in distance return abs(pos - self._position) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 263, in __sub__ return Vec2D(self[0]-other[0], self[1]-other[1]) TypeError: unsupported operand type(s) for -: 'tuple' and 'float'

2 answers

  • answered 2022-05-04 11:24 Rabbid76

    pygame.mouse.get_pos() has no arguments and returns a tuple. So either

    mouseX = pygame.mouse.get_pos()[0]
    mouseY = pygame.mouse.get_pos()[1]
    

    or

    mouseX, mouseY = pygame.mouse.get_pos()
    

  • answered 2022-05-04 11:33 The_spider

    turtle.distance returns the distance between the given coordinate and the turtle. You can pass in the arguments as a tuple (distance((x,y))) or as the x and y coordinate (distance(x,y)).

    This function isn't made to calculate the distance between two points and therefore, you shouldn't use it. It is designed to calculate the distance between the turtle and a coordinate. As you're passing in two arguments, turtle thinks taht the first argumpent is the x position and the second argument is the y coordinate. This obviously causes an error.

    You shouldn't be using turtle here at all. A function calculating the distance is very easy to implement:

    from math import sqrt
    def distance(pos1,pos2):
        return sqrt((pos1[0]-pos2[0])**2+(pos1[1]-pos2[1])**2)
    

    The program doesn't raise an error on the lines 1859 and 263 of your code, these lines are in the turtle module code. The error is raised within the turtle module code, but it does so because you provided the wrong arguments.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum