-1

How do you convert a [x][y] float back into a integer after converting Python 2.7 to 3.4 .that is formatted as a float

ex. self.table[x][y] into a integer to get the x and y values.

Error I get

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

What I tried

I know of the method of convert (x) into a integer like this int(x) or 5 / 3 into 5 //3 etc. This worked on converting all other times in the past I came into the problem so far. There was similar issue but those answers do not work in this case TypeError: integer argument expected got float' Python 3.4 Which I tried those methods as that was my question also.

I am using python 3.4 with pygame which might not matter for this example

What I am trying to do:

The goal is to convert a tutorial into from python 2.7 into python 3.4 to understand it using python 3.4. I ran 2to3.py to see if that would work as well.

the section of code where issue is

  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))

Bigger section encase its needed

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
Community
  • 1
  • 1
john taylor
  • 828
  • 10
  • 24
  • It's hard to know what you're asking because you're using some confusing undefined terms ("table", "table float"), but it sounds like you just want to convert floats into integers. How do you expect that to work? Do you want rounding or just truncation? Why is `int(x)` not good enough for some float `x`? – Two-Bit Alchemist Aug 28 '15 at 17:10
  • I was converting a tutorial written in python 2.7 into 3.4. I don't fully understand the code yet. But I understand the problem is about the [x] & [y] in python 3.4 being converted into a float. I tried self.table(int([x][y]).images etc but that did not work or look right. – john taylor Aug 28 '15 at 17:30

1 Answers1

1

int([x][y]) is a completely meaningless statement. [] is the indexing operator, for getting elements at a particular position in a list. Indexing thin air is nonsensical.

To convert the floating point numbers to indexing for indentation with int, you will have to do something like

self.table[int(x)][int(y)].images

or whatever the code is you want. You convert each floating point number into an integer individually. Note this is truncation not rounding: 2.99 will become 2.

Two-Bit Alchemist
  • 15,547
  • 5
  • 38
  • 74
  • thanks, I knew it looked wrong.. but that is easy to remember going forward. Only a few weeks into learning python. I fixed those plus 4 other documents in the includes which uncovered a bunch more unrelated errors but I will work through those. – john taylor Aug 28 '15 at 19:30