Python 3.7.1 Pygame 1.9.4错误:TypeError:需要整数参数,得到浮点
所以,我在制作一个非常简单的游戏。我打算绕一圈跳。在那之前,一切都很顺利。但当我尝试在游戏中添加跳跃时,出现了一个错误消息,告诉我,“TypeError:integer参数应为,Get float”我多次检查代码。我似乎找不到错误。所以我请求帮助。这是我的密码:Python 3.7.1 Pygame 1.9.4错误:TypeError:需要整数参数,得到浮点,python,pygame,Python,Pygame,所以,我在制作一个非常简单的游戏。我打算绕一圈跳。在那之前,一切都很顺利。但当我尝试在游戏中添加跳跃时,出现了一个错误消息,告诉我,“TypeError:integer参数应为,Get float”我多次检查代码。我似乎找不到错误。所以我请求帮助。这是我的密码: import pygame pygame.init() win = pygame.display.set_mode((500, 500)) pygame.display.set_caption("A GAME") screenWi
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("A GAME")
screenWidth = 500
x = 100
y = 400
width = 50
height = 50
vel = 10
r = 15
isJump = False
jumpCount = 10
run = True
clock = pygame.time.Clock()
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
clock.tick(60)
keys = pygame.key.get_pressed()
if keys [pygame.K_a] and x > r:
x -= vel
if keys [pygame.K_d] and x < screenWidth - r:
x += vel
if not (isJump):
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
if keys [pygame.K_ESCAPE]:
run = False
win.fill((0, 0, 0))
pygame.draw.circle(win, (255, 0, 0), (x, y), r, 0)
pygame.display.update()
pygame.quit()
导入pygame
pygame.init()
win=pygame.display.set_模式((500500))
pygame.display.set_标题(“游戏”)
屏幕宽度=500
x=100
y=400
宽度=50
高度=50
水平=10
r=15
isJump=False
跳数=10
运行=真
clock=pygame.time.clock()
运行时:
pygame.时间延迟(100)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
时钟滴答(60)
keys=pygame.key.get_pressed()
如果键[pygame.K_a]和x>r:
x-=水平
如果键[pygame.K_d]和x=-10:
负=1
如果跳线计数<0:
负=-1
y-=(跳线计数**2)*0.5*负
跳转计数-=1
其他:
isJump=False
跳数=10
如果键[pygame.K_ESCAPE]:
运行=错误
赢。填充((0,0,0))
pygame.draw.circle(赢,(255,0,0),(x,y),r,0)
pygame.display.update()
pygame.quit()
plz帮助此行:
pygame.draw.circle(win, (255, 0, 0), (x, y), r, 0)
应改为:
pygame.draw.circle(win, (255, 0, 0), (x, int(y)), r, 0)
由于y是该行后面的浮点:
y -= (jumpCount ** 2) * 0.5 * neg
或者你可以像这样修复它:
y -= int((jumpCount ** 2) * 0.5 * neg)
Rudy的答案将起作用,楼层划分(//)的输出为int:
y -= (jumpCount ** 2) // 2 * neg
代码中唯一的
浮点
是y
;你可能会想要像这样的东西
y -= int((jumpCount ** 2) * 0.5 * neg)
相反,使用0.5将y变成浮点。我想那不是你想要的。因此,将其转化为:
y -= (jumpCount * jumpCount) // 2 * neg
pygame.draw.circle(win,(255,0,0),(x,y),r,0)应该是pygame.draw.circle(win,(255,0,0),(x,int(y)),r,0)因为y是一个浮点数,所以只有一个浮点数:
0.5
。这就把整个支出变成了一个浮动。请改为尝试y-=(jumpCount*jumpCount)//2*neg
。