将Python 2.7转换为3.4后,将[x] [y] float转换回整数

时间:2015-08-28 17:05:58

标签: python-3.x pygame python-2to3

在将Python 2.7转换为3.4后,如何将[x] [y] float转换回整数。格式化为float

离。 self.table [x] [y]成一个整数来得到x和y值。

我遇到错误

return self.table[x][y].physics_module
TypeError: list indices must be integers, not float

我尝试了什么

我知道将(x)转换为像这个int(x)或5/3到5 // 3等整数的方法。这在过去所有其他时间转换我到目前为止遇到的问题。有类似的问题,但这些答案在这种情况下不起作用TypeError: integer argument expected got float' Python 3.4我尝试了那些方法,因为这也是我的问题。

我正在使用带有pygame的python 3.4,这对于这个例子可能无关紧要

我要做的是:

目标是将教程从python 2.7转换为python 3.4,以便使用python 3.4来理解它。我跑了2to3.py来看看它是否会起作用。

问题

的代码部分
  def makeGround(self, xa, ya, xb, yb):
        if (xa == xb):
            for y in range(ya, yb + 1):
                self.table[xa][y].makeGround()
        elif (ya == yb):
            for x in range(xa, xb + 1):
                self.table[x][ya].makeGround()

    def applyTileData(self, x, y, imageSequenceStack, passable, physics):
        self.table[x][y].images = imageSequenceStack
        self.table[x][y].passable = passable
        self.table[x][y].physics_module = physics

    def addPlatform(self, x, y, width):
        self.platforms.append((x, y, width))

更大的部分包含了所需的

import pygame
   from .Tile import *


    class TileSet:

    def __init__(self, width, height):
        cols = list(range(width))
        for i in range(width):
            cols[i] = list(range(height))
            for j in range(height):
                cols[i][j] = Tile(i, j, False)
        self.table = cols
        self.platforms = []
        self.background_color = (0, 0, 0)
        self.pixel_origin = (0, 0)
        self.background = None

    def setBackground(self, imageFile):
        self.background = imageFile

    def makeGround(self, xa, ya, xb, yb):
        if (xa == xb):
            for y in range(ya, yb + 1):
                self.table[xa][y].makeGround()
        elif (ya == yb):
            for x in range(xa, xb + 1):
                self.table[x][ya].makeGround()

    def applyTileData(self, x, y, imageSequenceStack, passable, physics):
        self.table[x][y].images = imageSequenceStack
        self.table[x][y].passable = passable
        self.table[x][y].physics_module = physics

    def addPlatform(self, x, y, width):
        self.platforms.append((x, y, width))

    def drawBackground(self, screen, topleft, frameNum):
        screen.fill(self.background_color)
        bg = self.background.getImageForFrame(frameNum)
        width = len(self.table) * 32
        height = len(self.table[0]) * 32
        pic_width = bg.get_width() - 640
        pic_height = bg.get_height() - 480
        left = 0
        top = 0
        if pic_width > 0 and width > 0:
            left = -1 * pic_width * topleft[0] / width
        if pic_height > 0 and height > 0:
            top = -1 * pic_height * topleft[1] / height

        screen.blit(bg, (left, top))


    def drawBottom(self, screen, camera, frameNum):
        top_left = self.fromPixelToTile(camera[0], camera[1])
        bottom_right = self.fromPixelToTile(camera[0] + 640, camera[1] + 480)
        left = min(len(self.table) - 1, max(0, top_left[0]))
        top = min(len(self.table[0]) - 1, max(0, top_left[1]))
        right = min(len(self.table) - 1, max(0, bottom_right[0]))
        bottom = min(len(self.table[0]) - 1, max(0, bottom_right[1]))

        for x in range(left, right + 1):
            for y in range(top, bottom + 1):
                topleft = self.fromTileToPixel(x, y)
                for imgSeq in self.table[x][y].images:
                    screen.blit(imgSeq.getImageForFrame(frameNum), (x * 32 - camera[0], y * 32 - camera[1]))


    def fromTileToPixel(self, tile_x, tile_y):
        x = 32 * (tile_x - self.pixel_origin[0])
        y = 32 * (tile_y - self.pixel_origin[1])
        return (x, y)

    def fromPixelToTile(self, pixel_x, pixel_y):
        x = (pixel_x / 32) + self.pixel_origin[0]
        y = (pixel_y / 32) + self.pixel_origin[1]
        return (x, y)

    def nearbyTiles(self, coordinate, radius = 1, tileFilter = None):
        tiles = []
        left = max(0, coordinate[0] - radius)
        right = min(coordinate[0] + radius, len(self.table) - 1)
        top = max(0, coordinate[1] - radius)
        bottom = min(coordinate[1] + radius, len(self.table[0]) - 1)
        for x in range(left, right + 1):
            for y in range(top, bottom + 1):
                tiles.append(self.table[x][y])

        if tileFilter:
            return list(filter(tileFilter, tiles))
        return tiles

    def getPhysicsModule(self, xy):
        x = xy[0]
        y = xy[1]
        x = max(0, x)
        x = min(x, len(self.table) - 1)
        y = max(0, y)
        y = min(y, len(self.table[0]) - 1)
        return self.table[x][y].physics_module

    def isTilePassable(self, xy):
        x = xy[0]
        y = xy[1]
        x = max(0, x)
        x = min(x, len(self.table) - 1)
        y = max(0, y)
        y = min(y, len(self.table[0]) - 1)
        return self.table[x][y].passable

1 个答案:

答案 0 :(得分:1)

int([x][y])是一个毫无意义的陈述。 []是索引运算符,用于将元素放在列表中的特定位置。索引稀薄的空气是荒谬的。

要将浮点数转换为使用int进行缩进索引,您必须执行类似

的操作
self.table[int(x)][int(y)].images

或者你想要的任何代码。您将每个浮点数分别转换为整数。请注意,这是截断而不是舍入:2.99将变为2.