TypeError:预期的整数参数,浮点数,带pygame的python 3.6.3
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TypeError:预期的整数参数,浮点数,带pygame的python 3.6.3相关的知识,希望对你有一定的参考价值。
我的python游戏有问题,这是我的第一个项目。我收到一个错误(TypeError:期望的整数参数,浮点数)。当我插入用户定义的功能拍摄时,它开始出现。如何解决这个问题或使SuperMario图像与类一起拍摄圆形对象。谢谢 :)
import pygame
import time
pygame.init()
display_height = 600
display_width = 800
white = (255,255,255)
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Igra1')
clock = pygame.time.Clock()
bg = pygame.image.load('background.jpg')
bg = pygame.transform.scale(bg,(800, 600))
Img_SuperMario = pygame.image.load('SuperMario.png')
SuperMario_width = 611
SuperMario_height = 611
Img_SuperMario = pygame.transform.scale(Img_SuperMario, (100, 100))
def shoot(xshoot):
while xshoot < display_width:
pygame.draw.circle(gameDisplay, white, (xshoot,50), 20 ,0)
xshoot += 10
def SuperMario(x,y):
gameDisplay.blit(Img_SuperMario,(x,y))
def gameloop():
x = (display_width * 0.1)
y = (display_height * 0.1)
x_change = 0
y_change = 0
game_exit = False
while not game_exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
elif event.key == pygame.K_UP:
y_change = -5
elif event.key == pygame.K_DOWN:
y_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_DOWN or event.key == pygame.K_UP:
x_change = 0
y_change = 0
if x == display_width - 100 or x == 0:
x_change = 0
if y == (display_height - 100) or y == 0:
y_change = 0
x += x_change
y += y_change
xshoot = x
gameDisplay.fill(white)
gameDisplay.blit(bg,(0,0))
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
while xshoot < display_width:
shoot(x)
SuperMario(x,y)
pygame.display.update()
clock.tick(60)
gameloop()
pygame.quit()
quit()
答案
pygame.draw.circle
函数只接受整数,但你的x
变量是一个浮点数。将xshoot
变量转换为shoot
函数中的整数,例如:
pygame.draw.circle(gameDisplay, white, (int(xshoot), 50), 20 ,0)
看起来你还需要在主循环和while xshoot < display_width:
函数中更改shoot
循环,但我不确定你想要实现什么。
另一答案
x = int((display_width * 0.1))
y = int((display_height * 0.1))
尝试解析到int,也许会工作。
以上是关于TypeError:预期的整数参数,浮点数,带pygame的python 3.6.3的主要内容,如果未能解决你的问题,请参考以下文章