I am using Python and wxPython to attempt to make a 2d game. When w is pressed, the tiles (except the character) should move so it looks like you are moving. The problem is, when the key is pressed, nothing moves. I added a line of code(no longer there) to make sure the tile position variable was changing, which it was. So, the position is not updating. Is there a way to reload the frame?

import wx
level=1
attack=1
defense=1
health=5
map4_31_locationx=270
map4_31_locationy=170
class MainWindow(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,size = (600, 400))

        self.panel = wx.Panel(self,-1)
        
        self.map4_31="C:/python27/game/4_31.gif"
        self.map1=wx.Image(self.map4_31,wx.BITMAP_TYPE_GIF).ConvertToBitmap()
        height=self.map1.GetHeight()
        width=self.map1.GetWidth()
        self.map1.map4_31 = wx.StaticBitmap(self.panel, -1, self.map1, (map4_31_locationx, map4_31_locationy), (width,height))

        self.playerFile = "C:/python27/game/player_tile.gif"
        self.playerbmp = wx.Image(self.playerFile,wx.BITMAP_TYPE_GIF).ConvertToBitmap()
        height=self.playerbmp.GetHeight()
        width=self.playerbmp.GetWidth()
        self.playerbmp.playerbitmap = wx.StaticBitmap(self.panel, -1, self.playerbmp, (270,170), (width,height))

        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyPress)
        
        self.Center()

    def moveup(self):
        global map4_31_locationy
        map4_31_locationy-=30
        
    def OnKeyPress(self,event):
        keycode=event.GetKeyCode()
        print keycode
        if keycode==87:
            MainWindow().moveup()

while True:
    app = wx.PySimpleApp()
    MainWindow().Show()
    app.MainLoop()

ok I got it working, but i have a different problem now. I am trying to load the position of the image, I open the file, save the first 3-digit number in the variable positionx, the second in positiony, and use rstrip("\r\n") to strip the new lines. Then, I use posisitonx and positiony as the coordinates, and I get this error:
TypeError: Expected a 2-tuple of integers or a wxPoint object.

import wx
level=1
attack=1
defense=1
health=5
app = wx.PySimpleApp()
class MainWindow(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,size = (600, 400))
        self.panel = wx.Panel(self,-1)
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyPress)
        self.Center()
        self.drawmap()
        try:
            filehandle = open ('location.txt', 'r')
        except IOError:
            filehandle = open ('location.txt', 'w')
            filehandle.write('270\r\n170')
            filehandle.close
            filehandle = open ('location.txt', 'r')
        filehandle.seek(0)
        positionx = filehandle.readline().rstrip("\n\r")
        positiony = filehandle.readline()
        self.char(positionx,positiony)
    def drawmap(self):
        self.map4_31="C:/python27/game/4_31.gif"
        self.map1=wx.Image(self.map4_31,wx.BITMAP_TYPE_GIF).ConvertToBitmap()
        height=self.map1.GetHeight()
        width=self.map1.GetWidth()
        self.map1.map4_31 = wx.StaticBitmap(self.panel, -1, self.map1, (270, 170), (width,height))
    def char(self,positionx,positiony):
        self.playerFile = "C:/python27/game/player_tile.gif"
        self.playerbmp = wx.Image(self.playerFile,wx.BITMAP_TYPE_GIF).ConvertToBitmap()
        height=self.playerbmp.GetHeight()
        width=self.playerbmp.GetWidth()
        self.playerbmp.playerbitmap = wx.StaticBitmap(self.panel, -1, self.playerbmp, (positionx,positiony), (width,height))
    def moveup(self):
        global playerpositiony
        playerpositiony+=30
        print playerpositiony
        self.char(playerpositiony)
        
    def OnKeyPress(self,event):
        keycode=event.GetKeyCode()
        print keycode
        if keycode==87:
            self.moveup()
if __name__ == "__main__":
    MainWindow()
    MainWindow().Show()
    app.MainLoop()

I tried replacing positionx and y in line 36 and it works fine that way, so that isn't the problem. The text file contains:
270
170
just like that

Edited 5 Years Ago by chrisvj11: n/a

I was thinking more along the lines of the module usage maybe something like this.

self.playerbmp.playerbitmap = wx.StaticBitmap(self.panel, -1, self.playerbmp, ((positionx,positiony), (width,height)))

Note: The extra brackets around (posx,posy),(width,height)

This article has been dead for over six months. Start a new discussion instead.