簡體   English   中英

嘗試通過播放器輸入使用tkinter使對象在網格中移動

[英]Trying to make an object move in a grid with tkinter through player inputs

因此,我正在開發類似tkinter而不是pygame的地下城爬行類游戲。 我發現這很困難,因為網格無法在代碼中工作,而且我找不到在畫布上創建可通過鍵輸入移動的形狀的方法。 我已經嘗試了多個修復程序,但無法實現所有解決方案。 如果我在制作網格時能找到一些幫助,該網格也允許形狀通過玩家的輸入來移動,這將在很大程度上幫助您。 這段代碼有很多錯誤,因為我仍然是一個新手,並且對於第一款游戲來說可能過於雄心勃勃。

from tkinter import*
import random

tk = Tk()


photo = PhotoImage(file="dungeon-wallpaper-1920x720.png")
label = Label(tk, image=photo)
label.pack()

def start(): #Starting Title
    print("You are a lone human who has ventured into a dangerous dungeon. "
    "The hallway is packed with monsters and is the only way out")
    print("Can your escape the Necromaner's Dungeon?")
    print("Developer's Note: Use python console for battle system and to check the introduction")
start = Button(text = "Start", command = start, bg = "Red", fg = "White")
start.pack()
def __init__(self, *args, **kwargs):
        tk.__init__(self, *args, **kwargs)
        self.canvas = tk.Canvas(self, width=500, height=500, borderwidth=0, highlightthickness=0)
        self.canvas.pack(side="top", fill="both", expand="true")
        self.rows = 100
        self.columns = 100
        self.cellwidth = 25
        self.cellheight = 25

        self.rect = {}
        self.oval = {}
        for column in range(20):
            for row in range(20):
                x1 = column*self.cellwidth
                y1 = row * self.cellheight
                x2 = x1 + self.cellwidth
                y2 = y1 + self.cellheight
                self.rect[row,column] = self.canvas.create_rectangle(x1,y1,x2,y2, fill="blue", tags="rect")
                self.oval[row,column] = self.canvas.create_oval(x1+2,y1+2,x2-2,y2-2, fill="blue", tags="oval")
        self.redraw(1000)

        def redraw(self, delay):
            self.canvas.itemconfig("rect", fill="blue")
            self.canvas.itemconfig("oval", fill="blue")
            for i in range(10):
                row = random.randint(0,19)
                col = random.randint(0,19)
                item_id = self.oval[row,col]
                self.canvas.itemconfig(item_id, fill="green")
                self.after(delay, lambda: self.redraw(delay))
def rightKey(event):
            print("Up key pressed")
def leftKey(event):
            print("Up key pressed")

def upKey(event):
            print("Up key pressed")
def downKey(event):
            print("Down key pressed")
tk.bind('<Up>', upKey)
tk.bind('<Down>', downKey)
tk.bind('<Left>', leftKey)
tk.bind('<Right>', rightKey)




def lootSys(): #LootSystem
   playerMoney = 0
   playerAttack = 10
   playerHealth = 100
   print("Input anything to take a turn. Input 1 to quit.")


   for i in range(0, 10):
       i += 1
       turn = input()
       if turn == str(1):
           break
       else:
           chance = random.randint(1, 100)
           if chance <= 5:
               playerMoney += 20
               print("Gold = " + str(playerMoney))
               print("Attack Damage = " + str(playerAttack))
               print("Health = " + str(playerHealth))
           elif chance <= 15:
               playerMoney += 10
               print("Gold = " + str(playerMoney))
               print("Attack Damage = " + str(playerAttack))
               print("Health = " + str(playerHealth))
           elif chance <= 30:
               playerMoney += 5
               print("Gold = " + str(playerMoney))
               print("Attack Damage = " + str(playerAttack))
               print("Health = " + str(playerHealth))
           elif chance <= 50:
               playerMoney += 1
               print("Gold = " + str(playerMoney))
               print("Attack Damage = " + str(playerAttack))
               print("Health = " + str(playerHealth))
           elif chance <= 70:
               playerAttack += 10
               print("Gold = " + str(playerMoney))
               print("Attack Damage = " + str(playerAttack))
               print("Health = " + str(playerHealth))
           elif chance <= 80:
               playerAttack += 20
               print("Gold = " + str(playerMoney))
               print("Attack Damage = " + str(playerAttack))
               print("Health = " + str(playerHealth))
           elif chance <= 100:
               playerHealth += 20
               print("Gold = " + str(playerMoney))
               print("Attack Damage = " + str(playerAttack))
               print("Health = " + str(playerHealth))


   print("Gold = " + str(playerMoney))  #L00T
   print("Attack Damage = " + str(playerAttack))
   print("Health = " + str(playerHealth))


def battleSys(): #Battle System
   playerHealth = 100
   print("Input anything to take a turn, or input 1 to stop.")


   for i in range(0, 10):
       i += 1
       turn = input()
       if turn == str(1):
           break
       else:
           chance = random.randint(1, 4)
           if chance == 1:
               playerHealth -= 10
               print("Health = " + str(playerHealth))
           elif chance == 2:
               playerHealth -= 20
               print("Health = " + str(playerHealth))
           elif chance == 3:
               playerHealth -= 40
               print("Health = " + str(playerHealth))
           else:
               playerHealth += 5
               print("Health = " + str(playerHealth))
       if playerHealth <= 0:
           print("Game Over")
           break
       else:
           continue
   if playerHealth > 0:
       print("Congratulations! You win!")
       lootSys()




battleSysAct = Button(text = "Battle System", command = battleSys, bg = "Blue", fg = "White")
battleSysAct.pack()



tk.mainloop()

如Bryan所說,您可以使用畫布的move方法。 下面是一個簡單的示例,其中播放器是一個紅色圓圈,您可以使用鍵盤箭頭在畫布上移動。 您可以向move_player函數添加其他鍵,以實現游戲中的其他動作。

import tkinter as tk

class Game(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.can = tk.Canvas(self, width=200, height=200)
        self.can.pack(fill="both", expand=True)
        self.player = self.can.create_oval(50,50,70,70, fill="red")
        self.bind("<Key>", self.move_player)

    def move_player(self, event):
        key = event.keysym
        if key == "Left":
            self.can.move(self.player, -20, 0)        
        elif key == "Right":
            self.can.move(self.player, 20, 0)    
        elif key == "Up":
            self.can.move(self.player, 0, -20)        
        elif key == "Down":
            self.can.move(self.player, 0, 20) 

if __name__ == '__main__':
    game = Game()
    game.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM