Warning (from warnings module): File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python38-32\rect 2.py", line 57 pygame.draw.rect(win,(0, 0, 255), (x, y, width, height)) 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.
This is the error I am getting from Shell and below is the code I typed:
import pygame
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("nyumph")
x = 50
y = 425
width = 40
height = 60
vel = 5
isJump = False
jumpCount = 10
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
if keys[pygame.K_RIGHT] and x < 500 - width - vel:
x += vel
if not(isJump):
if keys[pygame.K_UP] and y > vel:
y -= vel
if keys[pygame.K_DOWN] and y < 500 - height - vel:
y += vel
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -=1
else:
isJump = False
jumpCount = 10
win.fill((0, 0, 0))
pygame.draw.rect(win,(0, 0, 255), (x, y, width, height))
pygame.display.update()
pygame.quit()
This code is supposed to display a rectangle and make it jump on pressing the keys. I am not able to understand the error and what is the meaning of it and I also don't know what I'm supposed to do? The jumping is not working somehow. Please help me with it.
This is not an error, it is a warning. The warning is caused by the fact that the rectangle argument for pygame.draw.rect
has to be a tuple with integral values. In your application the coordinates (x
, y
) are floating point values.
You can get rid of the warning by rounding the x
and y
coordinate to integral values. Use the function round
:
pygame.draw.rect(win,(0, 0, 255), (x, y, width, height))
pygame.draw.rect(win,(0, 0, 255), (round(x), round(y), width, height))
The jumping does not work in your application because of the wrong Indentation in the if
statements.
See the working example:
import pygame
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("nyumph")
x = 50
y = 425
width = 40
height = 60
vel = 5
isJump = False
jumpCount = 10
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
if keys[pygame.K_RIGHT] and x < 500 - width - vel:
x += vel
#<--| INDENTATION
if not(isJump):
if keys[pygame.K_UP] and y > vel:
y -= vel
if keys[pygame.K_DOWN] and y < 500 - height - vel:
y += vel
if keys[pygame.K_SPACE]:
isJump = True
#<------| INDENTATION
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -=1
else:
isJump = False
jumpCount = 10
win.fill((0, 0, 0))
pygame.draw.rect(win,(0, 0, 255), (round(x), round(y), width, height))
pygame.display.update()
pygame.quit()