【发布时间】:2021-03-27 15:44:11
【问题描述】:
我正在尝试制作水果忍者游戏。我目前的目标是在我的画布上生成无限的落果(总共 4 列,随机速度),其中几个同时出现(不像在另一个消失后生成一个)。并跟踪它们中的每一个,以便稍后我可以进行碰撞(无论刀是否击中它们),如果是则摧毁它们。
之前,我尝试过root.after方法,但它显然不适用于我的代码,因为一旦再次调用该函数,该变量将被分配给一张新照片,因此之前的水果会消失。
我正在考虑在我展示的这个版本的代码中使用递归。不能正常工作:会生成水果,但是像after()方法一样,一旦到了点就会消失,只有在前一个水果消失后才能生成第二个水果。
这是我的代码:
from tkinter import *
import time
from random import randint
score = 0
def create_fruits():
while score < 21:
#choose random fruits with different possibilities
which_fruit = randint(1,11)
#import fuits and bomb images
global this_fruit
if which_fruit == 1 or which_fruit == 10 or which_fruit == 11:
this_fruit = PhotoImage(file = "bomb.png")
elif which_fruit == 2:
this_fruit = PhotoImage(file = "apple.png")
elif which_fruit == 3:
this_fruit = PhotoImage(file = "banana.png")
elif which_fruit == 4:
this_fruit = PhotoImage(file = "cherry.png")
elif which_fruit == 5:
this_fruit = PhotoImage(file = "blueberry.png")
elif which_fruit == 6:
this_fruit = PhotoImage(file = "pear.png")
elif which_fruit == 7:
this_fruit = PhotoImage(file = "lemon.png")
elif which_fruit == 8:
this_fruit = PhotoImage(file = "watermelon.png")
elif which_fruit == 9:
this_fruit = PhotoImage(file = "mango.png")
#randomly generate which colum (four in total) the fruits will be
choose_position = randint (1,4)
global x_position
if choose_position == 1:
x_position = 112.5
elif choose_position == 2:
x_position = 287.5
elif choose_position == 3:
x_position = 462.5
else:
x_position = 637.5
#place the fruit image on the canvas
global my_photo
my_photo = canvas.create_image (x_position,0, image = this_fruit, anchor = 's')
x_volocity = 0
y_volocity = randint (5,10)
while True:
coords = canvas.coords(my_photo) #get coordinates of the fruit picture
print (coords)
#move the fruit
canvas.move (my_photo,x_volocity,y_volocity)
canvas.update()
time.sleep(0.01)
if coords [1] >= 400:
create_fruits()
#create game area
root = Tk ()
canvas = Canvas (root, height = 1200, width = 800, bg = 'black')
#canvas.create_image(0,0, image = bg_image, anchor = 'nw')
canvas.pack()
create_fruits()
mainloop()
这是我尝试的另一个版本:(例如将第二个水果分配给一个新变量,然后调用递归)。结果是一旦第一个水果到达该点程序就会崩溃(就像没有响应一样)。
from tkinter import *
import time
from random import randint
score = 0
def create_fruits():
while score < 21:
#choose random fruits with different possibilities
which_fruit = randint(1,11)
#import fuits and bomb images
global this_fruit
if which_fruit == 1 or which_fruit == 10 or which_fruit == 11:
this_fruit = PhotoImage(file = "bomb.png")
elif which_fruit == 2:
this_fruit = PhotoImage(file = "apple.png")
elif which_fruit == 3:
this_fruit = PhotoImage(file = "banana.png")
elif which_fruit == 4:
this_fruit = PhotoImage(file = "cherry.png")
elif which_fruit == 5:
this_fruit = PhotoImage(file = "blueberry.png")
elif which_fruit == 6:
this_fruit = PhotoImage(file = "pear.png")
elif which_fruit == 7:
this_fruit = PhotoImage(file = "lemon.png")
elif which_fruit == 8:
this_fruit = PhotoImage(file = "watermelon.png")
elif which_fruit == 9:
this_fruit = PhotoImage(file = "mango.png")
#randomly generate which colum (four in total) the fruits will be
choose_position = randint (1,4)
global x_position
if choose_position == 1:
x_position = 112.5
elif choose_position == 2:
x_position = 287.5
elif choose_position == 3:
x_position = 462.5
else:
x_position = 637.5
#place the fruit image on the canvas
global my_photo
my_photo = canvas.create_image (x_position,0, image = this_fruit, anchor = 's')
x_volocity = 0
y_volocity = randint (5,10)
while True:
coords = canvas.coords(my_photo) #get coordinates of the fruit picture
print (coords)
#move the fruit
canvas.move (my_photo,x_volocity,y_volocity)
canvas.update()
time.sleep(0.01)
if coords [1] >= 400:
create_fruits()
#choose random fruits with different possibilities
which_fruit2 = randint(1,11)
#import fuits and bomb images
global this_fruit2
if which_fruit2 == 1 or which_fruit2 == 10 or which_fruit2 == 11:
this_fruit2 = PhotoImage(file = "bomb.png")
elif which_fruit == 2:
this_fruit2 = PhotoImage(file = "apple.png")
elif which_fruit == 3:
this_fruit2 = PhotoImage(file = "banana.png")
elif which_fruit == 4:
this_fruit2 = PhotoImage(file = "cherry.png")
elif which_fruit == 5:
this_fruit2 = PhotoImage(file = "blueberry.png")
elif which_fruit == 6:
this_fruit2 = PhotoImage(file = "pear.png")
elif which_fruit == 7:
this_fruit2 = PhotoImage(file = "lemon.png")
elif which_fruit == 8:
this_fruit2 = PhotoImage(file = "watermelon.png")
elif which_fruit == 9:
this_fruit2 = PhotoImage(file = "mango.png")
#randomly generate which colum (four in total) the fruits will be
choose_position2 = randint (1,4)
global x_position2
if choose_position2 == 1:
x_position = 112.5
elif choose_position2 == 2:
x_position2 = 287.5
elif choose_position2 == 3:
x_position2 = 462.5
else:
x_position2 = 637.5
#place the fruit image on the canvas
global my_photo2
my_photo2 = canvas.create_image (x_position2,0, image = this_fruit2, anchor = 's')
x_volocity2 = 0
y_volocity2 = randint (5,10)
while True:
coords2 = canvas.coords(my_photo2) #get coordinates of the fruit picture
print (coords2)
#move the fruit
canvas.move (my_photo2,x_volocity2,y_volocity2)
canvas.update()
time.sleep(0.01)
if coords2 >= 400:
create_fruits()
#create game area
root = Tk ()
canvas = Canvas (root, height = 1200, width = 800, bg = 'black')
#canvas.create_image(0,0, image = bg_image, anchor = 'nw')
canvas.pack()
create_fruits()
mainloop()
这是我的代码的第三个版本(使用after 方法)。在代码中,我还尝试使用 list 来跟踪水果并在之后删除它们,但它似乎不起作用(我不确定为什么)。
#make the fruits appear on the screen
def create_fruits():
#choose random fruits with different possibilities
which_fruit = randint(1,11)
#import fuits and bomb images
global this_fruit
if which_fruit == 1 or which_fruit == 10 or which_fruit == 11:
this_fruit = PhotoImage(file = "bomb.png")
elif which_fruit == 2:
this_fruit = PhotoImage(file = "apple.png")
elif which_fruit == 3:
this_fruit = PhotoImage(file = "banana.png")
elif which_fruit == 4:
this_fruit = PhotoImage(file = "cherry.png")
elif which_fruit == 5:
this_fruit = PhotoImage(file = "blueberry.png")
elif which_fruit == 6:
this_fruit = PhotoImage(file = "pear.png")
elif which_fruit == 7:
this_fruit = PhotoImage(file = "lemon.png")
elif which_fruit == 8:
this_fruit = PhotoImage(file = "watermelon.png")
elif which_fruit == 9:
this_fruit = PhotoImage(file = "mango.png")
#randomly generate which colum (four in total) the fruits will be
choose_position = randint (1,4)
global x_position
if choose_position == 1:
x_position = 112.5
elif choose_position == 2:
x_position = 287.5
elif choose_position == 3:
x_position = 462.5
else:
x_position = 637.5
#create a list to store fruit images
global fruits_list
fruits_list = []
#append it to the list
fruits_list.append(this_fruit)
#place the fruit image on the canvas
global my_photo
my_photo = canvas.create_image (x_position,0, image = this_fruit, anchor = 's')
#global root
#repeatedly generate fruit images every 3 second
root.after(3000,create_fruits)
#nake fruits' animation
#let one fruit keep moving while the second fruit pears
def move_fruits():
#set the moving speed
x_volocity = 0
y_volocity = randint (5,10)
#try to make two fruits moving at the same time on the screen
for i in range (0,len(fruits_list)-1):
while True:
coords = canvas.coords(fruits_list[i]) #get coordinates of the fruit picture
print (coords)
#move the fruit
canvas.move (fruits_list[i],x_volocity,y_volocity)
canvas.update()
time.sleep(0.01)
#assign this fruit to finished fruit to delete it from the list later
finished_fruit = fruits_list[i]
#check is the fruit is outside of the boarder, if so, delete if from the list and countinue on the second fruit
if coords [i] >= 1400:
fruits_list.remove(finished_fruit)
【问题讨论】:
-
你能显示带有
.after的版本吗?问题很可能是因为您使用了重用相同的变量。我认为最好将当前移动的水果附加到一个列表中以跟踪它们。 -
谢谢。我发布了代码的
after版本并尝试了列表(但我是编码新手,所以我真的不知道如何)
标签: python python-3.x tkinter tkinter-canvas