DeprecationWarning: an integer is required (got type float)

import pygame

pygame.init()

win = pygame.display.set_mode((500, 500))

pygame.display.set_caption("ДЕГРАДАЦИЯ")

x = 50
y = 400
weight = 40
height = 60
speed = 5

isJump = False
jumpCount = 10

lastMove = "right"

bullets = []

class snaryad():
    def __init__(self, x, y, radius, color, facing) -> object:
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.facing = facing
        self.vel = 8 * facing

    def draw(self, win):
        pygame.draw.circle(win, self.color, (self.x, self.y), self.radius)

def drawWindow():
    for bullet in bullets:
        bullet.draw(win)

run = True
while run:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    for bullet in bullets:
        if bullet.x < 500:
            if bullet.x > 0:
                bullet.x += bullet.vel
            else:
                bullets.pop(bullets.index(bullet))
        else:
            bullets.pop(bullets.index(bullet))

    keys = pygame.key.get_pressed()
    if keys [pygame.K_LEFT] and x > 5:
        x -= speed
        lastMove = "left"
    if keys [pygame.K_RIGHT] and x < 500 - weight - 5:
        x += speed
        lastMove = "right"
    if not isJump:
        if keys [pygame.K_SPACE]:
            isJump = True
    else:
        if jumpCount >= -10:
            if jumpCount < 0:
                y += (jumpCount ** 2) / 2
            else:
                y -= (jumpCount ** 2) / 2
            jumpCount -= 1
        else:
            isJump = False
            jumpCount = 10
    if keys [pygame.K_f]:
        if lastMove == "right":
            facing = 1
        else:
            facing = -1
        if len(bullets) < 3:
            bullets.append(snaryad(round(x + weight // 2), round(y + height // 2), 5, (255, 0, 0), facing))

    win.fill((0, 0, 0))
    pygame.draw.rect(win, (0, 0, 255), (x, y, weight, height))
    pygame.display.update()

pygame.quit()

Выходит ошибка:

[Running] python -u "c:\Users\Lenovo\Desktop\ДЕГРАДАЦИЯ.py"
pygame 1.9.6
c:\Users\Lenovo\Desktop\����������.py:86: DeprecationWarning: an integer is required (got type float).  Implicit conversion to integers using __int__ is deprecated and may be removed in a future version of Python.
  pygame.draw.rect(win, (0, 0, 255), (x, y, weight, height))
[Done] exited with code=0 in 11.009 seconds

Ответы (1 шт):

Автор решения: gil9red

У вас ошибка говорящая: an integer is required (got type float). Т.е. ожидается целое число, а получено вещественное.

Смотри в функцию, что в ошибке:

pygame.draw.rect(win, (0, 0, 255), (x, y, weight, height))

И сразу выделяем: x, y, weight, height, т.к. минимум, в одном из них есть вещественное число.

Я облегчу задачу и сразу скажу, что это y, из-за такого кода:

y += (jumpCount ** 2) / 2
y -= (jumpCount ** 2) / 2

Дело в том, что в питоне есть два оператора деления:

  • / -- деление с результатом как вещественное число, например: print(5 / 2) # 2.5
  • // -- деление с результатом как целое число, например: print(5 // 2) # 2

Поэтому, решением будет использование // вместо /:

y += (jumpCount ** 2) // 2
y -= (jumpCount ** 2) // 2

PS.

Подобные ошибки довольно легко находятся и исправляются, например добавив print можно понять с каким значениями имеем дело, а после сразу с ними кардинально разобраться, например:

x = int(x)
y = int(y)
...
pygame.draw.rect(win, (0, 0, 255), (x, y, weight, height))
→ Ссылка