Python 3.x tkinter - 如何在随机位置的画布上复制相同的sprite?

时间:2017-08-28 23:12:31

标签: python canvas tkinter tkinter-canvas

在我的代码中(将在长描述的末尾)我制作了一个tkinter画布游戏。到目前为止,画布上名为“播放器”的精灵向鼠标移动。当精灵“玩家”的坐标足够接近精灵“食物”的坐标时,食物将从画布上删除,然后在另一个随机位置再次创建。这也会将精灵“玩家”的大小增加1(通过在x1和y1坐标上加1)。

但我的问题是,如果没有一些非常奇怪的结果,我似乎无法创造超过1个稳定的“食物”精灵。理想情况下,我希望能够无限复制,直到我的CPU过热......但我会满足于10个精灵(但后来需要相同)。这样“玩家”精灵可以吃更多的食物。

import random
from tkinter import *
import math
import time

class Cells():

    def __init__(self):
        #Variables
        self.master = Tk()
        self.canvas = Canvas(self.master, width=1000,height=800)
        self.canvas.pack()
        self.player_colours = ['blue','red','orange','green','purple','yellow']
        self.random_int = random.randint(0,5)
        self.random_colour = self.player_colours[self.random_int]
        self.random_food_int = random.randint(0,5)
        self.random_food_colour = self.player_colours[self.random_food_int]
        self.tick_intervals = 10
        #Coords
        self.x0 = 500
        self.y0 = 400
        self.x1 = 530 
        self.y1 = 430
        self.rx0 = random.randint(100,900)
        self.ry0 = random.randint(100,700)
        self.rx1 = self.rx0 + 20
        self.ry1 = self.ry0 + 20
        self.rx0_2 = random.randint(100,900)
        self.ry0_2 = random.randint(100,700)
        self.rx1_2 = self.rx0_2 + 20
        self.ry1_2 = self.ry0_2 + 20
        self.player_size = self.x1 - self.x0
        self.player_coords = [self.x0, ",", self.y0]
        #Sprites
        self.speed = (self.x0 - self.x1) / 10
        self.reverse = self.speed - self.speed - self.speed
        self.food = self.canvas.create_oval(self.rx0,self.ry0,self.rx1,self.ry1,fill=self.random_food_colour)
        self.player = self.canvas.create_oval(self.x0,self.y0,self.x1,self.y1, fill=self.random_colour)
        self.player_coords_text_x = self.canvas.create_text(50,10,text=self.x0)
        self.player_coords_text_y = self.canvas.create_text(100,10,text=self.y0)
        self.food_coords_text_x = self.canvas.create_text(50,10,text=self.rx0_2)
        self.food_coords_text_y = self.canvas.create_text(100,10,text=self.ry0_2)
        self.player_text = self.canvas.create_text(self.x0,self.y0,text=self.player_size)
        self.food_count = 1
        self.move()
        self.master.mainloop()
    def move(self):
        self.canvas.delete(self.player_coords_text_x)
        self.canvas.delete(self.player_coords_text_y)
        self.player_coords_text_x = self.canvas.create_text(50,10,text=self.x0)
        self.player_coords_text_y = self.canvas.create_text(100,10,text=self.y0)

        self.canvas.delete(self.food_coords_text_x)
        self.canvas.delete(self.food_coords_text_y)
        self.food_coords_text_x = self.canvas.create_text(50,50,text=self.rx0_2)
        self.food_coords_text_y = self.canvas.create_text(100,50,text=self.ry0_2)


        self.is_food = True
        #mouse variables
        x = self.canvas.winfo_pointerx()-self.canvas.winfo_rootx()
        y = self.canvas.winfo_pointery()-self.canvas.winfo_rooty()
        self.mousex0 = x - 10
        self.mousey0 = y - 10
        self.mousex1 = x + 10
        self.mousey1 = y + 10
        #sprite variables
        self.player_center_x = self.x0 + ((self.x1 - self.x0) / 3)
        self.player_center_y = self.y0 + ((self.x1 - self.x0) / 3)

        self.canvas.delete(self.player_text)
        self.player_text = self.canvas.create_text(self.player_center_x,self.player_center_y,text=self.player_size)
        #bounderies
        if self.player_center_x < 20:
            self.x0 -=self.speed
            self.x1 -=self.speed
        elif self.player_center_x > 980:
            self.x0 += self.speed
            self.x1 += self.speed
        if self.player_center_y < 20:
            self.y0 -= self.speed
            self.y1 -= self.speed
        elif self.player_center_y > 780:
            self.y0 += self.speed
            self.y1 += self.speed
        #following the mouse
        if self.player_center_x < self.mousex0:
            self.x0 -= self.speed
            self.x1 -= self.speed
        elif self.player_center_x > self.mousex0:
            self.x0 += self.speed
            self.x1 += self.speed
        if self.player_center_y < self.mousey0:
            self.y0 -= self.speed
            self.y1 -= self.speed
        elif self.player_center_y > self.mousey0:
            self.y0 += self.speed
            self.y1 += self.speed
        #eating the food
        if self.is_food == True:
            if self.x0 < self.rx0 and (self.rx0 - self.x0) <= self.player_size:
                if self.y0 < self.ry0 and (self.ry0 - self.y0) <= self.player_size:
                    self.canvas.delete(self.food)
                    self.player_size += 1
                    self.x1 += 1
                    self.y1 += 1
                    self.is_food = False
            if self.x0 > self.rx0 and (self.x0 - self.rx0) <= self.player_size:
                if self.y0 < self.ry0 and (self.ry0 - self.y0) <= self.player_size:
                    self.canvas.delete(self.food)
                    self.player_size += 1
                    self.x1 += 1
                    self.y1 += 1
                    self.is_food = False
            if self.x0 > self.rx0 and (self.x0 - self.rx0) <= self.player_size:
                if self.y0 > self.ry0 and (self.y0 - self.ry0) <= self.player_size:
                    self.canvas.delete(self.food)
                    self.player_size += 1
                    self.x1 += 1
                    self.y1 += 1
                    self.is_food = False
            if self.x0 < self.rx0 and (self.rx0 - self.x0) <= self.player_size:
                if self.y0 > self.ry0 and (self.y0 - self.ry0) <= self.player_size:
                    self.canvas.delete(self.food)
                    self.player_size += 1
                    self.x1 += 1
                    self.y1 += 1
                    self.is_food = False
        if self.is_food == True:
            if self.player_center_x < self.rx0_2 and (self.rx0_2 - self.player_center_x) <= self.player_size:
                if self.player_center_y < self.ry0_2 and (self.ry0_2 - self.player_center_y) <= self.player_size:
                   self.canvas.delete(self.food)
                   self.player_size += 1
                   self.x1 += 1
                   self.y1 += 1
                    self.is_food = False
            elif self.player_center_x > self.rx0_2 and (self.player_center_x - self.rx0_2) <= self.player_size:
               if self.player_center_y < self.ry0_2 and (self.ry0_2 - self.player_center_y) <= self.player_size:
                    self.canvas.delete(self.food)
                    self.player_size += 1
                    self.x1 += 1
                    self.y1 += 1
                    self.is_food = False
            elif self.player_center_x > self.rx0_2 and (self.player_center_x - self.rx0_2) <= self.player_size:
                if self.player_center_y > self.ry0_2 and (self.ry0_2 + self.player_center_y) <= self.player_size:
                    self.canvas.delete(self.food)
                    self.player_size += 1
                    self.x1 += 1
                    self.y1 += 1
                    self.is_food = False
            elif self.player_center_x < self.rx0_2 and (self.player_center_x + self.rx0_2) <= self.player_size:
                if self.player_center_y > self.ry0_2 and (self.ry0_2 + self.player_center_y) <= self.player_size:
                    self.canvas.delete(self.food)
                    self.player_size += 1
                    self.x1 += 1
                    self.y1 += 1
                    self.is_food = False

        if self.is_food == False:
            self.rx0_2 = random.randint(100,900)
            self.ry0_2 = random.randint(100,700)
            self.rx1_2 = self.rx0_2 + 20
            self.ry1_2 = self.ry0_2 + 20
            self.food = self.canvas.create_oval(self.rx0_2,self.ry0_2,self.rx1_2,self.ry1_2,fill=self.random_food_colour)
            self.is_food = True

这个Guys是我对如何复制相同精灵的最初想法的来源。但如果你自己运行它,你会发现结果是......有问题的。 请帮我把这个工作做好。

        #if self.food_count <= 10:
            #self.food = self.canvas.create_oval(self.rx0_2,self.ry0_2,self.rx1_2,self.ry1_2,fill=self.random_food_colour)
            #self.food_count += 1

        #loop
        self.canvas.coords(self.player,self.x0,self.y0,self.x1,self.y1)
        self.canvas.after(self.tick_intervals,self.move)
if __name__ == '__main__':
    Cells()

1 个答案:

答案 0 :(得分:0)

请参阅下面的答案:

from tkinter import *
import random

class App:
    def __init__(self, root):
        self.root = root
        self.label = []
        self.text = []
        self.root.geometry("500x500")
        self.canvas = Canvas(root, width=500, height=500)
        self.button = Button(self.canvas, text="Ok", command=self.random)
        self.canvas.pack(expand=True, fill="both")
        self.button.pack()
    def random(self):
        self.label.append(Label(self.canvas, text="text"))
        for i in self.label:
            self.text.append(self.canvas.create_window(random.randrange(500), random.randrange(500), window=i))

root = Tk()
App(root)
root.mainloop()

在上文中,我们创建了两个list s labeltext

每次推送button时,我们都会创建一个新的label并将其存储在列表label中。然后,我们对list label进行迭代,并为列表中的每个元素创建一个新的canvas窗口,将这些窗口存储在text中以供日后使用。

这将允许您继续生产食物,直到CPU过热。

作为附注,我建议你回顾一下:

https://stackoverflow.com/help/mcve

您提供了太多不相关的代码,您的问题归结为“我如何随机化画布中多个对象的位置?”然而,你已经为你的玩家角色包含了整个逻辑。