BTW我正在使用Python 3 ...所以,当你点击箭头键时我需要进行角色移动。它是如何工作的:所以,我运行它,一切都很酷!但当我点击箭头键(左右两侧,它是一个2D游戏)时,没有任何反应。如果有人愿意提供帮助,我们将不胜感激!所以,这是我的代码。 全部:
from tkinter import *
from random import *
from time import *
health = 5
InvSeleSlot = 1
inInv = 'No'
window = Tk()
c = Canvas(window, bg='blue', height=1000, width=1000)
HeartsImage = PhotoImage(file = "/home/tyty0427/Downloads/New Piskel(4).png")
Heart = c.create_image(100, 100, image=HeartsImage)
Heart2 = c.create_image(200, 100, image=HeartsImage)
Heart3 = c.create_image(300, 100, image=HeartsImage)
Heart4 = c.create_image(400, 100, image=HeartsImage)
Heart5 = c.create_image(500, 100, image=HeartsImage)
DeadHeartImage = PhotoImage(file = "/home/tyty0427/Downloads/New Piskel(5).png")
DeadHeart = c.create_image(10000, 10000, image=DeadHeartImage)
characterSprite = PhotoImage(file = '/home/tyty0427/Downloads/New Piskel.gif')
character = c.create_image(500, 510, image=characterSprite)
if health == 6:
Heart6 = c.create_image(600, 100, image=HeartsImage)
if health == 7:
Heart6 = c.create_image(600, 100, image=HeartsImage)
Heart7 = c.create_image(700, 100, image=HeartsImage)
if health == 8:
Heart6 = c.create_image(600, 100, image=HeartsImage)
Heart7 = c.create_image(700, 100, image=HeartsImage)
Heart8 = c.create_image(800, 100, image=HeartsImage)
if health == 9:
Heart6 = c.create_image(600, 100, image=HeartsImage)
Heart7 = c.create_image(700, 100, image=HeartsImage)
Heart8 = c.create_image(800, 100, image=HeartsImage)
Heart9 = c.create_image(900, 100, image=HeartsImage)
if health == 10:
Heart6 = c.create_image(600, 100, image=HeartsImage)
Heart7 = c.create_image(700, 100, image=HeartsImage)
Heart8 = c.create_image(800, 100, image=HeartsImage)
Heart9 = c.create_image(900, 100, image=HeartsImage)
Heart10 = c.create_image(1000, 100, image=HeartsImage)
damageTooken = 0
if health == 10:
if damageTooken == 10:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
c.itemconfig(heart2, state=HIDDEN)
c.itemconfig(heart, state=HIDDEN)
dead = 'Yes'
if damageTooken == 9:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
c.itemconfig(heart2, state=HIDDEN)
if damageTooken == 8:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
if damageTooken == 7:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
if damageTooken == 6:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
if damageTooken == 5:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
if damageTooken == 4:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
if damageTooken == 3:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
if damageTooken == 2:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
if damageTooken == 1:
c.itemconfig(Heart10, state=HIDDEN)
if health == 9:
if damageTooken == 10:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
c.itemconfig(heart2, state=HIDDEN)
c.itemconfig(heart, state=HIDDEN)
dead = 'Yes'
if damageTooken == 9:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
c.itemconfig(heart2, state=HIDDEN)
dead = 'Yes'
if damageTooken == 8:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
if damageTooken == 7:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
if damageTooken == 6:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
if damageTooken == 5:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
if damageTooken == 4:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
if damageTooken == 3:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
if damageTooken == 2:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
if damageTooken == 1:
c.itemconfig(Heart10, state=HIDDEN)
if health == 8:
if damageTooken == 10:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
c.itemconfig(heart2, state=HIDDEN)
c.itemconfig(heart, state=HIDDEN)
dead = 'Yes'
if damageTooken == 9:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
c.itemconfig(heart2, state=HIDDEN)
dead = 'Yes'
if damageTooken == 8:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
dead = 'Yes'
if damageTooken == 7:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
if damageTooken == 6:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
if damageTooken == 5:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
if damageTooken == 4:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
if damageTooken == 3:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
if damageTooken == 2:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
if damageTooken == 1:
c.itemconfig(Heart10, state=HIDDEN)
if health == 7:
if damageTooken == 10:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
c.itemconfig(heart2, state=HIDDEN)
c.itemconfig(heart, state=HIDDEN)
dead = 'Yes'
if damageTooken == 9:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
c.itemconfig(heart2, state=HIDDEN)
dead = 'Yes'
if damageTooken == 8:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
dead = 'Yes'
if damageTooken == 7:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
dead = 'Yes'
if damageTooken == 6:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
if damageTooken == 5:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
if damageTooken == 4:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
if damageTooken == 3:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
if damageTooken == 2:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
if damageTooken == 1:
c.itemconfig(Heart10, state=HIDDEN)
if health == 6:
if damageTooken == 10:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
c.itemconfig(heart2, state=HIDDEN)
c.itemconfig(heart, state=HIDDEN)
dead = 'Yes'
if damageTooken == 9:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
c.itemconfig(heart2, state=HIDDEN)
dead = 'Yes'
if damageTooken == 8:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
dead = 'Yes'
if damageTooken == 7:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
dead = 'Yes'
if damageTooken == 6:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
dead = 'Yes'
if damageTooken == 5:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
if damageTooken == 4:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
if damageTooken == 3:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
if damageTooken == 2:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
if damageTooken == 1:
c.itemconfig(Heart10, state=HIDDEN)
if health == 5:
if damageTooken == 10:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
c.itemconfig(heart2, state=HIDDEN)
c.itemconfig(heart, state=HIDDEN)
dead = 'Yes'
if damageTooken == 9:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
c.itemconfig(heart2, state=HIDDEN)
dead = 'Yes'
if damageTooken == 8:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
c.itemconfig(heart3, state=HIDDEN)
dead = 'Yes'
if damageTooken == 7:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
c.itemconfig(heart4, state=HIDDEN)
dead = 'Yes'
if damageTooken == 6:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
c.itemconfig(Heart5, state=HIDDEN)
dead = 'Yes'
if damageTooken == 5:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
c.itemconfig(Heart6, state=HIDDEN)
dead = 'Yes'
if damageTooken == 4:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
c.itemconfig(Heart7, state=HIDDEN)
if damageTooken == 3:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
c.itemconfig(Heart8, state=HIDDEN)
if damageTooken == 2:
c.itemconfig(Heart10, state=HIDDEN)
c.itemconfig(Heart9, state=HIDDEN)
if damageTooken == 1:
c.itemconfig(Heart10, state=HIDDEN)
InventorySlot1 = c.create_rectangle(50, 900, 80, 930, activefill='yellow', fill='gray')
InventorySlot2 = c.create_rectangle(80, 900, 110, 930, activefill='yellow', fill='gray')
InventorySlot3 = c.create_rectangle(110, 900, 140, 930, activefill='yellow', fill='gray')
InventorySlot4 = c.create_rectangle(140, 900, 170, 930, activefill='yellow', fill='gray')
InventorySlot5 = c.create_rectangle(170, 900, 200, 930, activefill='yellow', fill='gray')
InventorySlot6 = c.create_rectangle(200, 900, 230, 930, activefill='yellow', fill='gray')
InventorySlot7 = c.create_rectangle(230, 900, 260, 930, activefill='yellow', fill='gray')
InventorySlot8 = c.create_rectangle(260, 900, 290, 930, activefill='yellow', fill='gray')
InventorySlot9 = c.create_rectangle(290, 900, 320, 930, activefill='yellow', fill='gray')
InventorySlot10 = c.create_rectangle(320, 900, 350, 930, activefill='yellow', fill='gray')
WhatInSlot1 = 'Nothing'
WhatInSlot2 = 'Nothing'
WhatInSlot3 = 'Nothing'
WhatInSlot4 = 'Nothing'
WhatInSlot5 = 'Nothing'
WhatInSlot6 = 'Nothing'
WhatInSlot7 = 'Nothing'
WhatInSlot8 = 'Nothing'
WhatInSlot9 = 'Nothing'
WhatInSlot10 = 'Nothing'
def SelectSlot1(event):
InvSeleSlot = 1
c.itemconfigure(InventorySlot1, fill='yellow')
def SelectSlot2(event):
InvSeleSlot = 2
c.itemconfigure(InventorySlot2, fill='yellow')
def SelectSlot3(event):
InvSeleSlot = 3
c.itemconfigure(InventorySlot3, fill='yellow')
def SelectSlot4(event):
InvSeleSlot = 4
c.itemconfigure(InventorySlot4, fill='yellow')
def SelectSlot5(event):
InvSeleSlot = 5
c.itemconfigure(InventorySlot5, fill='yellow')
def SelectSlot6(event):
InvSeleSlot = 6
c.itemconfigure(InventorySlot6, fill='yellow')
def SelectSlot7(event):
InvSeleSlot = 7
c.itemconfigure(InventorySlot7, fill='yellow')
def SelectSlot8(event):
InvSeleSlot = 8
c.itemconfigure(InventorySlot8, fill='yellow')
def SelectSlot9(event):
InvSeleSlot = 9
c.itemconfigure(InventorySlot9, fill='yellow')
def SelectSlot10(event):
InvSeleSlot = 10
c.itemconfigure(InventorySlot10, fill='yellow')
c.bind("F1", SelectSlot1)
c.bind("F2", SelectSlot2)
c.bind("F3", SelectSlot3)
c.bind("F4", SelectSlot4)
c.bind("F5", SelectSlot5)
c.bind("F6", SelectSlot6)
c.bind("F7", SelectSlot7)
c.bind("F8", SelectSlot8)
c.bind("F9", SelectSlot9)
c.bind("F10", SelectSlot10)
TreeSizeList = ['300', '100', '200', '400']
TreeSize = choice(TreeSizeList)
TreeSize2 = choice(TreeSizeList)
Tree1Wood = c.create_rectangle(200, TreeSize, 250, 580, fill='brown')
Tree1Leaves = c.create_oval(150, TreeSize, 300, 410, fill='green')
Tree2Wood = c.create_rectangle(600, TreeSize2, 650, 580, fill='brown')
Tree2Leaves = c.create_oval(550, TreeSize2, 700, 410, fill='green')
ChooseNewBiomeList = ['No', 'No', 'No', 'No', 'No', 'Yes']
StartingWorldEndRight = 1000
StartingWorldEndLeft = 0
StartingWorld = c.create_rectangle(0, 580, 1000, 1000, fill='green')
CharSpeed = 10
def move_right(event):
ClickedRightArrow = ClickedRightArrow + 1
c.move(character, CharSpeed, 0)
ChooseNewBiome = random.choice(ChoiceNewBiomeList)
c.move(Tree1Wood, -10, 0)
c.move(Tree1Leaves, -10, 0)
c.move(Tree2Wood, -10, 0)
c.move(Tree2Leaves, -10, 0)
c.move(StartingWorld, -10, 0)
if ChooseNewBiome == 'Yes':
NewBiomeList = ['Snowy', 'Desert', 'Grass']
NewBiome = random.choice(NewBiomeList)
if NewBiome == 'Snowy':
SnowyBiome = c.create_rectangle(StartingWorldEndRight - ClickedRightArrow + 10, 580, 1000, 1000, fill='white')
if NewBiome == 'Desert':
SnowyBiome = c.create_rectangle(StartingWorldEndRight - ClickedRightArrow + 10, 580, 1000, 1000,fill='Yellow')
if NewBiome == 'Snowy':
SnowyBiome = c.create_rectangle(StartingWorldEndRight - ClickedRightArrow + 10, 580, 1000, 1000, fill='green')
def move_left(event):
ClickedRightArrow = ClickedRightArrow + 1
c.move(character, -CharSpeed, 0)
ChooseNewBiome = random.choice(ChoiceNewBiomeList)
c.move(Tree1Wood, 10, 0)
c.move(Tree1Leaves, 10, 0)
c.move(Tree2Wood, 10, 0)
c.move(Tree2Leaves, 10, 0)
c.move(StartingWorld, 10, 0)
if ChooseNewBiome == 'Yes':
NewBiomeList = ['Snowy', 'Desert', 'Grass']
NewBiome = random.choice(NewBiomeList)
if NewBiome == 'Snowy':
SnowyBiome = c.create_rectangle(0, 580, StartingWorldEndRight - ClickedRightArrow + 10, 1000, fill='white')
if NewBiome == 'Desert':
SnowyBiome = c.create_rectangle(0, 580, StartingWorldEndRight - ClickedRightArrow + 10, 1000,fill='Yellow')
if NewBiome == 'Snowy':
SnowyBiome = c.create_rectangle(0, 580, StartingWorldEndRight - ClickedRightArrow + 10, 1000, fill='green')
def EventBindings(event):
c.bind("<Right>", move_right)
c.bind("<Left>", move_left)
c.bind("<Key>", EventBindings)
c.pack()
window.mainloop()
我关注的两位代码:
def move_right(event):
ClickedRightArrow = ClickedRightArrow + 1
c.move(character, CharSpeed, 0)
ChooseNewBiome = random.choice(ChoiceNewBiomeList)
c.move(Tree1Wood, -10, 0)
c.move(Tree1Leaves, -10, 0)
c.move(Tree2Wood, -10, 0)
c.move(Tree2Leaves, -10, 0)
c.move(StartingWorld, -10, 0)
if ChooseNewBiome == 'Yes':
NewBiomeList = ['Snowy', 'Desert', 'Grass']
NewBiome = random.choice(NewBiomeList)
if NewBiome == 'Snowy':
SnowyBiome = c.create_rectangle(StartingWorldEndRight - ClickedRightArrow + 10, 580, 1000, 1000, fill='white')
if NewBiome == 'Desert':
SnowyBiome = c.create_rectangle(StartingWorldEndRight - ClickedRightArrow + 10, 580, 1000, 1000,fill='Yellow')
if NewBiome == 'Snowy':
SnowyBiome = c.create_rectangle(StartingWorldEndRight - ClickedRightArrow + 10, 580, 1000, 1000, fill='green')
def move_left(event):
ClickedRightArrow = ClickedRightArrow + 1
c.move(character, -CharSpeed, 0)
ChooseNewBiome = random.choice(ChoiceNewBiomeList)
c.move(Tree1Wood, 10, 0)
c.move(Tree1Leaves, 10, 0)
c.move(Tree2Wood, 10, 0)
c.move(Tree2Leaves, 10, 0)
c.move(StartingWorld, 10, 0)
if ChooseNewBiome == 'Yes':
NewBiomeList = ['Snowy', 'Desert', 'Grass']
NewBiome = random.choice(NewBiomeList)
if NewBiome == 'Snowy':
SnowyBiome = c.create_rectangle(0, 580, StartingWorldEndRight - ClickedRightArrow + 10, 1000, fill='white')
if NewBiome == 'Desert':
SnowyBiome = c.create_rectangle(0, 580, StartingWorldEndRight - ClickedRightArrow + 10, 1000,fill='Yellow')
if NewBiome == 'Snowy':
SnowyBiome = c.create_rectangle(0, 580, StartingWorldEndRight - ClickedRightArrow + 10, 1000, fill='green')
def EventBindings(event):
c.bind("<Right>", move_right)
c.bind("<Left>", move_left)
c.bind("<Key>", EventBindings)
(其中一些是针对世界一代的东西。这个游戏非常类似于Terraria,(但是,不完全)) 下一部分代码(字符精灵和创建的代码)
characterSprite = PhotoImage(file = '/home/tyty0427/Downloads/New Piskel.gif')
character = c.create_image(500, 510, image=characterSprite
)
答案 0 :(得分:1)
Canvas c
没有键盘焦点,因此永远不会触发c
上的键绑定。要捕获关键的新闻事件,您可以在window
上进行绑定。
另外,我发现绑定的方式很奇怪:
def EventBindings(event):
c.bind("<Right>", move_right)
c.bind("<Left>", move_left)
c.bind("<Key>", EventBindings)
我不明白为什么你需要EventBindings
函数,它基本上在每次按下一个键时重做所有键绑定。只是做:
window.bind("<Right>", move_right)
window.bind("<Left>", move_left)