Dismiss Notice
Wynncraft, the Minecraft MMORPG. Play it now on your Minecraft client at (IP): play.wynncraft.com. No mods required! Click here for more info...

Coding Help

Discussion in 'Other Games' started by VillagerFilms, Jan 21, 2017.

Thread Status:
Not open for further replies.
  1. VillagerFilms

    VillagerFilms I like music HERO

    Messages:
    298
    Likes Received:
    254
    Trophy Points:
    72
    Minecraft:
    So I am coding in python and I am using tkinter to make a game. But even if I type window.mainloop() AND c.pack() there are 0 errors but the tkinter window STILL doesn't show up! A little help please.
    ________________________________
    Here is my code (remember this: I am not very familiar with classes):
    Code:
    from Tkinter import *
    HEIGHT = 1000
    WIDTH = 1000
    window = Tk()
    window.title('Castle Jumper')
    c = Canvas(window, width=WIDTH, height=HEIGHT, bg='gray')
    c.pack()
    player_id = c.create_oval(5, 5, 5, 25, fill='blue')
    PLAYER_R = 15
    MID_X = WIDTH / 2
    MID_Y = HEIGHT / 2
    c.move(player_id, MID_X, MID_Y)
    player_spd = 10
    player_spd2 = 100
    onSolid = 'False'
    def move_character(event):
        if event.keysym == 'Up':
            c.move(player_id, 0 -player_spd)
            sleep(2)
            c.move(player_id, 0, player_spd)
        elif event.keysym == 'Left':
            c.move(player_id, -player_spd, 0)
        elif event.keysym == 'Right':
            c.move(player_id, player_id, 0)
    
    c.bind_all('<Key>', move_character)
    box_id = c.create_rectangle(400, 525, 600, 600, fill='black')
    BOX_R = 200
    def get_coords(id_num):
        pos = c.coords(id_num)
        x = (pos[0] + pos[2])
        y = (pos[1] + pos[3])
        return x, y
    from math import sqrt
    def distance(id1, id2):
        x1, y1 = get_coords(id1)
        x2, y2 = get_coords(id2)
        return sqrt((x2 - x1)**2 + (y2 - y1)**2)
    
    def collision():
            if distance(player_id, box_id) < (PLAYER_R + BOX_R):
                onSolid = 'True'
            else:
                onSolid = 'False'
    #Main Game Loop
    while True:
        collision()
        if onSolid == 'False':
            c.move(player_id, 0, player_spd2)
    c.pack()
    window.mainloop(n=0)
    
    
     
  2. memethyl

    memethyl the king of shitposting VIP+

    Messages:
    404
    Likes Received:
    2,242
    Trophy Points:
    73
    Guild:
    Minecraft:
    okay i'm not sure what this game is but i'm pretty sure you already fucked up

    if you're looking into making a little game in python, you're gonna wanna use pygame:
    http://www.pygame.org

    otherwise, i'd need to see the actual code to see what your problem is
     
  3. VillagerFilms

    VillagerFilms I like music HERO

    Messages:
    298
    Likes Received:
    254
    Trophy Points:
    72
    Minecraft:
    This game is a 2D-Platformer where you have to do parkour from castle to castle and at the end of each castle is a boss. And i showed you the code.
    ________________________________
    I am trying it without pygame. I am trying to challenge myself.
     
  4. memethyl

    memethyl the king of shitposting VIP+

    Messages:
    404
    Likes Received:
    2,242
    Trophy Points:
    73
    Guild:
    Minecraft:
    Code:
    from tkinter import *
    HEIGHT = 1000
    WIDTH = 1000
    window = Tk()
    window.title('Castle Jumper')
    c = Canvas(window, width=WIDTH, height=HEIGHT, bg='gray')
    c.pack()
    player_id = c.create_oval(5, 5, 5, 25, fill='blue')
    PLAYER_R = 15
    MID_X = WIDTH / 2
    MID_Y = HEIGHT / 2
    c.move(player_id, MID_X, MID_Y)
    player_spd = 10
    player_spd2 = 100
    onSolid = 'False'
    def move_character(event):
        if event.keysym == 'Up':
            c.move(player_id, 0 -player_spd)
            sleep(2)
            c.move(player_id, 0, player_spd)
        elif event.keysym == 'Left':
            c.move(player_id, -player_spd, 0)
        elif event.keysym == 'Right':
            c.move(player_id, player_id, 0)
    
    c.bind_all('<Key>', move_character)
    box_id = c.create_rectangle(400, 525, 600, 600, fill='black')
    BOX_R = 200
    def get_coords(id_num):
        pos = c.coords(id_num)
        x = (pos[0] + pos[2])
        y = (pos[1] + pos[3])
        return x, y
    from math import sqrt
    def distance(id1, id2):
        x1, y1 = get_coords(id1)
        x2, y2 = get_coords(id2)
        return sqrt((x2 - x1)**2 + (y2 - y1)**2)
    
    def collision():
            if distance(player_id, box_id) < (PLAYER_R + BOX_R):
                onSolid = 'True'
            else:
                onSolid = 'False'
    #Main Game Loop
    while True:
        window.mainloop(n=0)
        collision()
        if onSolid == 'False':
            c.move(player_id, 0, player_spd2)
    c.pack()
    
    there, pretty sure i fixed it
    i put window.mainloop() in your main game loop, rather than making it the last instruction of the program.

    also, perhaps look into classes in the future, it'd make things a lot cleaner
     
  5. VillagerFilms

    VillagerFilms I like music HERO

    Messages:
    298
    Likes Received:
    254
    Trophy Points:
    72
    Minecraft:
    Thanks so much! It worked! :D Again, Thanks!
     
  6. VillagerFilms

    VillagerFilms I like music HERO

    Messages:
    298
    Likes Received:
    254
    Trophy Points:
    72
    Minecraft:
    oh. The reason it didn't appear is because I typed time.sleep() somewhere. And now I need to use time.sleep()
     
Thread Status:
Not open for further replies.