python - 我的 pygame 游戏中出现 "TypeError: integer argument expected, got float"的原因是什么?

标签 python linux pygame

我正在尝试使用 pygame 库编写游戏,但由于某种原因,它不断抛出以下行的 TypeError:需要整数参数,得到 float 错误:

if surface.get_at((player["x"], player["y"] + player["height"])) == (0,0,0,255):
leftOfPlayerOnPlatform = False


if surface.get_at((player["x"] + player["width"], player["y"] + player["height"])) == (0,0,0,255):
rightOfPlayerOnPlatform = False


if leftOfPlayerOnPlatform is False and rightOfPlayerOnPlatform is False and (player["y"] + player["height"]) + player["vy"] < windowHeight:
player["y"] += player["vy"]

我的问题的完整代码是:

import pygame, sys, random
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EVENTS
import pygame.time as GAME_TIME

pygame.init()
clock = pygame.time.Clock()

title_image = pygame.image.load("assets/title.jpg")
game_over_image = pygame.image.load("assets/game_over.jpg")

windowWidth = 400
windowHeight = 600

surface = pygame.display.set_mode((windowWidth, windowHeight))
pygame.display.set_caption('Drop!')

leftDown = False
rightDown = False

gameStarted = False
gameEnded = False
gamePlatforms = []
platformSpeed = 3
platformDelay = 2000
lastPlatform = 0
platformsDroppedThrough = -1
dropping = False

gameBeganAt = 0
timer = 0

player = {
  "x" : windowWidth / 2,
  "y" : 0,
  "height" : 25,
  "width" : 10,
  "vy" : 5
}

def drawPlayer():

  pygame.draw.rect(surface, (255,0,0), (player["x"], player["y"],player["width"], player["height"]))

def movePlayer():

  global platformsDroppedThrough, dropping

  leftOfPlayerOnPlatform = True
  rightOfPlayerOnPlatform = True

  if surface.get_at((player["x"], player["y"] + player["height"])) == (0,0,0,255):
    leftOfPlayerOnPlatform = False

  if surface.get_at((player["x"] + player["width"], player["y"] + player["height"])) == (0,0,0,255):
    rightOfPlayerOnPlatform = False

  if leftOfPlayerOnPlatform is False and rightOfPlayerOnPlatform is False and (player["y"] + player["height"]) + player["vy"] < windowHeight:
    player["y"] += player["vy"]

    if dropping is False:
      dropping = True
      platformsDroppedThrough += 1

  else :

    foundPlatformTop = False
    yOffset = 0
    dropping = False

    while foundPlatformTop is False:

      if surface.get_at((player["x"], (player["y"] + player["height"]) - yOffset )) == (0,0,0,255):
        player["y"] -= yOffset
        foundPlatformTop = True
      elif (player["y"] + player["height"]) - yOffset > 0:
        yOffset += 1
      else :

        gameOver()
        break

  if leftDown is True:
    if player["x"] > 0 and player["x"] - 5 > 0:
      player["x"] -= 5
    elif player["x"] > 0 and player["x"] - 5 < 0:
      player["x"] = 0

  if rightDown is True:
    if player["x"] + player["width"] < windowWidth and (player["x"] + player["width"]) + 5 < windowWidth:
      player["x"] += 5
    elif player["x"] + player["width"] < windowWidth and (player["x"] + player["width"]) + 5 > windowWidth:
      player["x"] = windowWidth - player["width"]

def createPlatform():

  global lastPlatform, platformDelay

  platformY = windowHeight
  gapPosition = random.randint(0, windowWidth - 40)

  gamePlatforms.append({"pos" : [0, platformY], "gap" : gapPosition})
  lastPlatform = GAME_TIME.get_ticks()

  if platformDelay > 800:
    platformDelay -= 50

def movePlatforms():
  # print("Platforms")

  for idx, platform in enumerate(gamePlatforms):

    platform["pos"][1] -= platformSpeed

    if platform["pos"][1] < -10:
      gamePlatforms.pop(idx)


def drawPlatforms():

  for platform in gamePlatforms:

    pygame.draw.rect(surface, (255,255,255), (platform["pos"][0], platform["pos"][1], windowWidth, 10))
    pygame.draw.rect(surface, (0,0,0), (platform["gap"], platform["pos"][1], 40, 10) )


def gameOver():
  global gameStarted, gameEnded

  platformSpeed = 0
  gameStarted = False
  gameEnded = True

def restartGame():

  global gamePlatforms, player, gameBeganAt, platformsDroppedThrough, platformDelay

  gamePlatforms = []
  player["x"] = windowWidth / 2
  player["y"] = 0
  gameBeganAt = GAME_TIME.get_ticks()
  platformsDroppedThrough = -1
  platformDelay = 2000

def quitGame():
  pygame.quit()
  sys.exit()

# 'main' loop
while True:

  surface.fill((0,0,0))

  for event in GAME_EVENTS.get():

    if event.type == pygame.KEYDOWN:

      if event.key == pygame.K_LEFT:
        leftDown = True
      if event.key == pygame.K_RIGHT:
        rightDown = True
      if event.key == pygame.K_ESCAPE:
        quitGame()

    if event.type == pygame.KEYUP:
      if event.key == pygame.K_LEFT:
        leftDown = False
      if event.key == pygame.K_RIGHT:
        rightDown = False

      if event.key == pygame.K_SPACE:
        if gameStarted == False:
          restartGame()
          gameStarted = True

    if event.type == GAME_GLOBALS.QUIT:
      quitGame()

  if gameStarted is True:
    # Play game
    timer = GAME_TIME.get_ticks() - gameBeganAt

    movePlatforms()
    drawPlatforms()
    movePlayer()
    drawPlayer()

  elif gameEnded is True:
    # Draw game over screen
    surface.blit(game_over_image, (0, 150))

  else :
    # Welcome Screen
    surface.blit(title_image, (0, 150))

  if GAME_TIME.get_ticks() - lastPlatform > platformDelay:
    createPlatform()

  clock.tick(60)
pygame.display.update()

我使用 Thonny 来运行代码。 如果有人可以帮助我解决我的问题,我将非常感激。

最佳答案

您使用的是 Python 2.7 还是 3.x?

如果您使用的是 3.x,它会默认应用浮点除法,因此该行

player["x"] = windowWidth / 2

将产生一个 float 。 PyGame 要求所有坐标均为整数。在 Python 3.x 中使用//进行整数除法

player["x"] = windowWidth // 2  #  or use
player["x"] = int(windowWidth / 2)

这很可能就是 TypeError: integer argument Expected, got float 的含义,因为您指示的行确实会引用坐标。

关于python - 我的 pygame 游戏中出现 "TypeError: integer argument expected, got float"的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50159527/

相关文章:

python - 为什么我的程序没有显示我告诉它的碰撞框?

python - 如何在 numpy 中获取给定大小的所有子矩阵?

python - 用 Pandas 替换数据框中不同列的值

linux - 从用户空间创建物理内存以用于 DMA 传输

c - 链接到目录后执行程序时崩溃

python - 按下回车键后如何让字符串出现

java - 为Python/Pygame程序员用java制作图形窗口

Python wave 模块仅适用于 v2.7,不适用于 v3.4 linux

javascript - 模板未在 Django 提供的 Vue 中呈现

linux - 使用 sudo 在单个命令中运行 "who am i"不会返回任何内容