【发布时间】:2018-06-07 10:44:38
【问题描述】:
我的 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()
【问题讨论】:
-
尝试合并下次出现错误的行。这将使跟踪错误更容易。
-
总是把完整的错误信息(Traceback)放在有问题的地方(作为文本,而不是屏幕截图)。还有其他有用的信息。
-
错误消息应该告诉你问题出在哪里,然后你应该使用
print()和print(type(...))检查变量中的值和类型以找到具有float值的变量,然后你应该使用int(). -
您需要搜索关于 SO 上的项目符号的教程或其他答案。您使用其他 while 循环的方法将不起作用。一个非常基本的解决方案是将子弹位置存储在列表中,并将子弹速度添加到每帧的位置。使用
for循环来更新和绘制子弹。