Python:如何在画布上获得记分板

时间:2013-07-19 09:25:38

标签: python python-3.x tkinter tkinter-canvas

我希望得分能够在画布上展示但到目前为止无济于事。我以为我可以使用:

self.canvas.create_text(200, 60, fill="darkblue", font="Times 15 italic bold", text="Your score is:",self.score)

不幸的是没有工作,所以如何在画布上显示分数?还想知道如何让一个消息框弹出说“你赢了!”当得分= 10时。最后,如何让弹出一个消息框说“如果得分<10并且画布上没有偶数,你就输了。”

目前使用python 3.3的代码:

        def click(self, event):
        if self.canvas.find_withtag(CURRENT):
            item_uid = event.widget.find_closest(event.x, event.y)[0]
             is_even = False
             try:  # clicked oval
                self.bubbles[item_uid]
            except KeyError:  # clicked the text
                for key, value in self.bubbles.iteritems():
                    if item_uid == value[5]:  # comparing to text_id
                        if value[4] % 2 == 0:
                            is_even = True
                        self.canvas.delete(key)  # deleting oval
                        self.canvas.delete(item_uid)  # deleting text
            else:
                if self.bubbles[item_uid][4] % 2 == 0:
                    is_even = True
                self.canvas.delete(item_uid)  # deleting oval
                self.canvas.delete(self.bubbles[item_uid][5])  # deleting text
            if is_even:
                self.score += 1
            else:
                self.score -= 1

            if self.score == 10:
                print ("You won")

        print (self.score)

2 个答案:

答案 0 :(得分:1)

这是一款有趣的游戏!我在下面给出的脚本向您展示了如何做你想做的一切(显示当前分数和显示消息)。其上方有##的行是我添加的:

from tkinter import *
import random
##
from tkinter.messagebox import showinfo

class BubbleFrame:

    def __init__(self, root):
        root.title("Math Bubbles")
        self.bubbles = {}
        self.score = 0
        Button(root, text="Start", width=8, command=self.initialize_bubbles).pack()  # This button starts the game, making the bubbles move across the screen
        Button(root, text="Quit", width=8, command=quit).pack()
        self.canvas = Canvas(root, width=800, height=650, bg='#afeeee')
        self.canvas.create_text(400, 30, fill="darkblue", font="Times 20 italic bold", text="Click the bubbles that are multiples of two.")
        ##
        self.current_score = self.canvas.create_text(200, 60, fill="darkblue", font="Times 15 italic bold", text="Your score is:")
        self.canvas.pack()

    def initialize_bubbles(self):
        for each_no in range(1, 21):
            xval = random.randint(5, 765)
            yval = random.randint(5, 615)
            oval_id = self.canvas.create_oval(xval, yval, xval + 30, yval + 30,fill="#00ffff", outline="#00bfff", width=5, tags="bubble")
            text_id = self.canvas.create_text(xval + 15, yval + 15, text=each_no, tags="bubble")
            self.canvas.tag_bind("bubble", "<Button-1>", lambda x: self.click(x))
            self.bubbles[oval_id] = (xval, yval, 0, 0, each_no, text_id)  

    def click(self, event):
        if self.canvas.find_withtag(CURRENT):
            item_uid = event.widget.find_closest(event.x, event.y)[0]
            is_even = False
            try:  
                self.bubbles[item_uid]
            except KeyError:  
                for key, value in self.bubbles.iteritems():
                    if item_uid == value[5]:  
                        if value[4] % 2 == 0:
                            is_even = True
                        self.canvas.delete(key)
                        self.canvas.delete(item_uid)
            else:
                if self.bubbles[item_uid][4] % 2 == 0:
                    is_even = True
                self.canvas.delete(item_uid)
                self.canvas.delete(self.bubbles[item_uid][5])
            if is_even:
                self.score += 1
            else:
                self.score -= 1

            if self.score == 10:
                ##
                showinfo("Winner", "You won!")
        ##
        self.canvas.delete(self.current_score)
        ##
        self.current_score = self.canvas.create_text(200, 60, fill="darkblue", font="Times 15 italic bold", text="Your score is: %s"%self.score)

    def loop(self, root):
        for oval_id, (x, y, dx, dy, each_no, text_id) in self.bubbles.items():
            dx += random.randint(-1, 1)
            dy += random.randint(-1, 1)
            dx, dy = max(-5, min(dx, 5)), max(-5, min(dy, 5))
            if not 0 < x < 770:
                dx = -dx
            if not 0 < y < 620:
                dy = -dy
            self.canvas.move(oval_id, dx, dy)
            self.canvas.move(text_id, dx, dy)
            self.bubbles[oval_id] = (x + dx, y + dy, dx, dy, each_no, text_id)
        root.after(100, self.loop, root)

if __name__ == "__main__":
    root = Tk()
    frame = BubbleFrame(root)
    frame.loop(root)
    root.mainloop()

答案 1 :(得分:0)

来自http://www.tutorialspoint.com/python/tk_messagebox.htm

import tkMessageBox

tkMessageBox.showinfo("Winner", "You won!")

您可以使用place而不是pack将画布上方的元素放在同一个Frame或Tk中。

Label(root, text="Quit").place()

<强>地方()

  • relx,depend:水平和垂直偏移为0.0到1.0之间的浮点数,作为父窗口小部件高度和宽度的一部分。

  • x,y:水平和垂直偏移(以像素为单位)。

来自http://www.tutorialspoint.com/python/tk_place.htm