在将 Python 2.7 转换为 3.4 后将 [x][y] 浮点数转换回整数
Converting a [x][y] float back into a integer after converting Python 2.7 to 3.4
如何在将 Python 2.7 转换为 3.4 后将 [x][y] 浮点数转换回整数。格式化为浮点数
例如。 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 我尝试了这些方法,因为这也是我的问题。
我正在使用 python 3.4 和 pygame,这对于这个例子可能无关紧要
我想做什么:
目标是将教程从 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
int([x][y])
是一个完全没有意义的声明。 []
是索引运算符,用于获取列表中特定位置的元素。索引稀薄的空气是荒谬的。
要使用 int
将浮点数转换为缩进索引,您必须执行类似
的操作
self.table[int(x)][int(y)].images
或者任何你想要的代码。您将 每个浮点数 分别转换为整数。请注意,这是 截断 而不是四舍五入:2.99 将变为 2.
如何在将 Python 2.7 转换为 3.4 后将 [x][y] 浮点数转换回整数。格式化为浮点数
例如。 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 我尝试了这些方法,因为这也是我的问题。
我正在使用 python 3.4 和 pygame,这对于这个例子可能无关紧要
我想做什么:
目标是将教程从 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
int([x][y])
是一个完全没有意义的声明。 []
是索引运算符,用于获取列表中特定位置的元素。索引稀薄的空气是荒谬的。
要使用 int
将浮点数转换为缩进索引,您必须执行类似
self.table[int(x)][int(y)].images
或者任何你想要的代码。您将 每个浮点数 分别转换为整数。请注意,这是 截断 而不是四舍五入:2.99 将变为 2.