Class 如何在tkinter制作记分板

Class 如何在tkinter制作记分板,class,tkinter,pygame,tkinter-canvas,Class,Tkinter,Pygame,Tkinter Canvas,我想用tkinter实现一个记分板。 如果目标子弹和目标敌人之间的距离小于10,我想增加10分。 如何添加代码? 先谢谢你 from tkinter import * import time import random WIDTH = 800 HEIGHT = 800 class Ball: def __init__(self, canvas, color, size, x, y, xspeed, yspeed): self.canvas = canvas

我想用tkinter实现一个记分板。 如果目标子弹和目标敌人之间的距离小于10,我想增加10分。 如何添加代码? 先谢谢你

from tkinter import *
import time
import random

WIDTH = 800
HEIGHT = 800

class Ball:
    def __init__(self, canvas, color, size, x, y, xspeed, yspeed):
        self.canvas = canvas
        self.color = color
        self.size = size
        self.x = x
        self.y = y
        self.xspeed = xspeed
        self.yspeed = yspeed
        self.id = canvas.create_oval(x, y, x+size, y+size, fill=color)

    def move(self):
        self.canvas.move(self.id, self.xspeed, self.yspeed)
        (x1, y1, x2, y2) = self.canvas.coords(self.id)
        (self.x, self.y) = (x1, y1)
        if x1 <= 0 or x2 >= WIDTH:  
            self.xspeed = - self.xspeed
        if y1 <= 0 or y2 >= HEIGHT: 
            self.yspeed = - self.yspeed


bullets = []

def fire(event):
    bullets.append(Ball(canvas, "red", 10, 150, 250, 10, 0))

def up(event):
    spaceship.yspeed-=1
def down(event):
    spaceship.yspeed+=1


window = Tk()
canvas = Canvas(window, width=WIDTH, height=HEIGHT)
canvas.pack()
canvas.bind("<Button-1>", fire)
window.bind("<Up>",up)
window.bind("<Down>",down) 


spaceship = Ball(canvas, "green", 100, 100, 200, 0, 0)
enemy = Ball(canvas, "red", 100, 500, 200, 5, 0)


while True:
    for bullet in bullets:
        bullet.move()
        if (bullet.x+bullet.size) >= WIDTH: 
            canvas.delete(bullet.id)
            bullets.remove(bullet)
    enemy.move()
    spaceship.move()
    window.update()
    time.sleep(0.03)
从tkinter导入*
导入时间
随机输入
宽度=800
高度=800
班级舞会:
定义初始化(自身、画布、颜色、大小、x、y、x速度、y速度):
self.canvas=画布
self.color=颜色
self.size=大小
self.x=x
self.y=y
self.xspeed=xspeed
self.yspeed=yspeed
self.id=canvas.create_oval(x,y,x+size,y+size,fill=color)
def移动(自我):
self.canvas.move(self.id、self.xspeed、self.yspeed)
(x1,y1,x2,y2)=自画布坐标(自id)
(self.x,self.y)=(x1,y1)
如果x1=宽度:
self.xspeed=-self.xspeed
如果y1=高度:
self.yspeed=-self.yspeed
项目符号=[]
def火灾(事件):
项目符号。附加(球(画布,“红色”,10150250,10,0))
def up(事件):
宇宙飞船。Y速度-=1
def关闭(事件):
宇宙飞船。Y速度+=1
window=Tk()
画布=画布(窗口,宽度=宽度,高度=高度)
canvas.pack()
canvas.bind(“,fire)
窗口绑定(“,向上)
window.bind(“,向下)
太空船=球(画布,“绿色”,100100200,0,0)
敌人=球(画布,“红色”,1005002005,0)
尽管如此:
对于子弹中的子弹:
子弹,移动
如果(bullet.x+bullet.size)>=宽度:
canvas.delete(bullet.id)
项目符号。移除(项目符号)
敌人,行动
太空船移动
window.update()
睡眠时间(0.03)

有很多方法可以改进您的程序,但我将只关注冲突方面

Tkinter canvas有几种方法
find_closest
find_enclosed
find_overlapping
()允许您检测对象之间的关系<代码>查找最近的将是我的首选

find_closest
应采用“敌人”的x、y坐标和“光晕距离”(小于10像素)。这将返回附近对象的id。如果其中一个物体是子弹,那么在分数上加10分

还有其他需要解决的问题

  • 您没有tkinter.mainloop。你应该
  • 你通过改变速度而不是xy坐标来移动宇宙飞船的方法很差,最终会导致一个非常快速移动的宇宙飞船

既然在代码中没有使用“pygame”,为什么要将问题标记为“pygame”?您还应该添加“python”标记。此外,我在代码中没有看到任何实现记分板的尝试。如果您向我们展示您所做的事情并告诉我们您的困境,而不是要求他人为您编写代码,您将获得更多帮助。见和。