【问题标题】:Tkinter moving a png on canvas using key pressTkinter 使用按键在画布上移动 png
【发布时间】:2019-12-03 12:42:19
【问题描述】:

我必须制作一个小游戏作为功课。 我正在尝试使用 python 和 tkinter 重拍 Zelda 游戏(此处必须使用 tkinter)。

这是我的问题,我可以显示窗口、地牢的墙壁和地面以及角色林克,但我找不到使用键盘让林克移动的方法。

这是我的完整代码

#Imports
from tkinter import *


#Fonctions

def new_game():
    """Fonction qui lance le jeu, avec en premier le canvas de fond suivis du canvas de sol du dongeon"""
    fenetre.after(1, frame_wall)
    fenetre.after(2, canvas_donjon)



def canvas_donjon():
#Ici est le canvas de la pièce du donjon sur lequel s'affiche Link
    global link
    global dungeon_ground
    dungeon_ground = Canvas(fenetre, width=roomx, height=roomy, bg = 'green', bd=0, highlightthickness=0)
    dungeon_ground.grid(row=0, column=0, rowspan= 14, columnspan=9)
    link = PhotoImage(file="LinksDown1.png")
    dungeon_ground.create_image(x,y, anchor=NW, image=link)



def menu_title():
    """Fonction lancant l'image du menu"""
    titreZelec = PhotoImage(file = ".\Image_Menu\TitreZelectromeca.png")
    canvas.create_image(menux/2, menuy/2, image = titreZelec)
    menu_title.grid(row = 0, column = 1)


def frame_wall():
    """Fonction plaçant l'image mur dans le premier canvas de fond"""
    mur_contour = PhotoImage(file = ".\Map\MurContours.png")
    canvas.create_image(wallx/2, wally/2, image = mur_contour)
    frame_wall.grid(row = 0, column = 1)

def presskey(event):
    global link
    global x
    global y
    if event.char == "q":
        dungeon_ground.move(link, -10, 0)
    elif event.char == "d":
        dungeon_ground.move(link, 10, 0)
    elif event.char == "z":
        dungeon_ground.move(link, 0, -10)
    elif event.char == "s":
        dungeon_ground.move(link, 0, 10) 


# Données

life_point = 3

black_cornerx = 213.5
black_cornery = 180.5

roomx = 660
roomy = 385

menux = 549
menuy = 413

wallx = 869
wally = 562

pathx = 55
pathy = 55

geox = 869
geoy = 650
geo = "%ix%i" % (geox,geoy)

persox = 255
persoy = 255

x = 40 #position de départ du perso
y = 40


#Création de la fenêtre

fenetre = Tk()
fenetre.config(background = 'black')
fenetre.geometry(geo)
fenetre.title('The legend of Zélectromécabilouschtroumpfos')
fenetre.iconbitmap(".\Images_Menu\Logo.ico")


# Création des boutons
demarrerJeu = Button(fenetre, text='Start adventure !', command = new_game)
quitterJeu = Button(fenetre, text='Quitter', command = fenetre.destroy)

label_test = Label(fenetre, text = "Life :" + life_point*"O")


# Création première grille
canvas = Canvas(fenetre, width=wallx, height=wally, bg = 'black', bd = 0, highlightthickness=0)
canvas.grid(row=0,column=0,rowspan= 2,columnspan=3)


#Placement dans la grille

demarrerJeu.grid(row = 3, column = 0)
quitterJeu.grid(row = 3, column = 2)
label_test.grid(row = 2, column =1)

fenetre.bind("<Key>", presskey)

# Afficher la fenêtre
fenetre.mainloop()

这里是创建地牢和角色的函数

def canvas_donjon():
#Ici est le canvas de la pièce du donjon sur lequel s'affiche Link
    global link
    global dungeon_ground
    dungeon_ground = Canvas(fenetre, width=roomx, height=roomy, bg = 'green', bd=0, highlightthickness=0)
    dungeon_ground.grid(row=0, column=0, rowspan= 14, columnspan=9)
    link = PhotoImage(file="LinksDown1.png")
    dungeon_ground.create_image(x,y, anchor=NW, image=link)

这里是管理按键事件的函数

def presskey(event):
    global link
    global x
    global y
    if event.char == "q":
        dungeon_ground.move(link, -10, 0)
    elif event.char == "d":
        dungeon_ground.move(link, 10, 0)
    elif event.char == "z":
        dungeon_ground.move(link, 0, -10)
    elif event.char == "s":
        dungeon_ground.move(link, 0, 10)

我希望它足够清楚,我是python和Stackoverflow的新手......我感觉有点不知所措。

提前致谢!

【问题讨论】:

    标签: python tkinter key move


    【解决方案1】:

    没关系,今天早上我顿悟了。

    我认为,变量“link”用于引用png文件,这与画布上显示的图像对象不同。 所以,我创建了一个新变量:

    def canvas_donjon():
    #Ici est le canvas de la pièce du donjon sur lequel s'affiche Link
        global link
        global img #added this line to be able to use the img variable elsewhere
        global dungeon_ground
        dungeon_ground = Canvas(fenetre, width=roomx, height=roomy, bg = 'green', bd=0, highlightthickness=0)
        dungeon_ground.grid(row=0, column=0, rowspan= 14, columnspan=9)
        link = PhotoImage(file="LinksDown1.png")
        # img is the new variable
        img = dungeon_ground.create_image(x,y, anchor=NW, image=link)
    

    这个 img 变量是指在画布上创建的图像。 我在按键功能中使用了它:

    def presskey(event):
        global img #here I replaced 'link' with 'img'
        global x
        global y
        if event.char == "q":
            dungeon_ground.move(img, -10, 0) #.move now uses img instead of link
        elif event.char == "d":
            dungeon_ground.move(img, 10, 0)
        elif event.char == "z":
            dungeon_ground.move(img, 0, -10)
        elif event.char == "s":
            dungeon_ground.move(img, 0, 10)
    

    现在可以正常使用了!我希望它会帮助别人!

    干杯

    【讨论】:

      猜你喜欢
      • 2014-01-05
      • 2018-03-05
      • 1970-01-01
      • 2011-12-05
      • 2023-03-08
      • 2017-05-30
      • 1970-01-01
      • 2014-11-05
      • 2021-06-28
      相关资源
      最近更新 更多