0 Replies - 208 Views - Last Post: 19 July 2014 - 09:33 PM Rate Topic: -----

#1 grimofdoom  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 14-August 12

Tkinter game, object not acting right(bound buttons)

Posted 19 July 2014 - 09:33 PM

I am using Tkinter with python 2.7. In my game, I am trying to make a test map with objects that cannot be stepped into and have movable character using arrows. I came up with a simple procedure of checking if spot is withing object(using left-right-top-button of object), if the spot the character is going to go to is part of the object, it will do nothing. If the spot isn't part of an object, it will move in the right direction until it cannot or the player stops/changes direction.
My current problem is that when i go left and hit an object, i cannot move up or down, but just to the right(reverse). and same with going down and hitting an object, except i cannot move left or right.
Here is my current code and picture files(avatar and map)(sorry if it looks a little messy, I tried to keep it as small as possible and clean as possible.)
import Tkinter as tk

win=tk.Tk()
walk=tk.Toplevel(win)


def callback(event):
    main.focus_set()

def move_up(event):
    avatar_spot=main.coords(main_avatar)
    x=avatar_spot[0]
    y=avatar_spot[1]
    x+=0
    y-=2
    j=0
    for i in objects:
            collision=objects[j]
            x1=collision[0]
            x1-=16
            y1=collision[2]
            y1-=20
            if x>x1 and x<collision[1]:
                if y>y1 and y<collision[3]:
                    break
            j+=1
    else:
        main.coords(main_avatar,(x+0),(y-2))
def move_down(event):
    avatar_spot=main.coords(main_avatar)
    x=avatar_spot[0]
    y=avatar_spot[1]
    x+=0
    y+=2
    j=0
    for i in objects:
            collision=objects[j]
            x1=collision[0]
            x1-=16
            y1=collision[2]
            y1-=20
            if x>x1 and x<collision[1]:
                if y>y1 and y<collision[3]:
                    break
            j+=1
    else:
        main.coords(main_avatar,(x+0),(y+2))
def move_left(event):
    avatar_spot=main.coords(main_avatar)
    x=avatar_spot[0]
    y=avatar_spot[1]
    x-=2
    y+=0
    j=0
    for i in objects:
            collision=objects[j]
            x1=collision[0]
            x1-=16
            y1=collision[2]
            y1-=20
            if x>x1 and x<collision[1]:
                if y>y1 and y<collision[3]:
                    break
            j+=1
    else:
        main.coords(main_avatar,(x-2),(y+0))
def move_right(event):
    avatar_spot=main.coords(main_avatar)
    x=avatar_spot[0]
    y=avatar_spot[1]
    x+=2
    y+=0
    j=0
    for i in objects:
            collision=objects[j]
            x1=collision[0]
            x1-=16
            y1=collision[2]
            y1-=20
            if x>x1 and x<collision[1]:
                if y>y1 and y<collision[3]:
                    break
            j+=1
    else:
        main.coords(main_avatar,(x+2),(y+0))

mapbg=tk.PhotoImage(file='map1.gif')
main=tk.Canvas(walk,height=320,width=320)
main.pack(expand=False,side='left')
main_image=main.create_image(0,0, image=mapbg, anchor='nw')
main.bind('<Button-1>',callback)
main.bind('<Up>',move_up)
main.bind('<Down>',move_down)
main.bind('<Left>',move_left)
main.bind('<Right>',move_right)

objects=[
        (0,0,0,0),
        (80,145,130,192)
    ]
avatar=tk.PhotoImage(file='sprite1.gif')
main_avatar=main.create_image(160,160,image=avatar,anchor='nw')

win.mainloop()


(i placed the sprite and map both in the attached .zip file.)

I could really use some help here, ive re-written it a few times and it still produces problems.(first version,worst of all, bounced back if you hit within object space- which had bug where you could get ontop of objects)

Attached File(s)


This post has been edited by grimofdoom: 19 July 2014 - 09:36 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1