问题描述
所以我对 python 还是很陌生,并试图通过制作小项目来学习.我正在制作的游戏旨在通过创建一堆随机圆圈来测试您的鼠标准确性,玩家应该在给定的时间内单击这些圆圈.在游戏结束时,它应该告诉玩家他们的分数,以及他们有多少误点击.
So I'm still very new to python and trying to learn through making small projects.The game I'm making is meant to test your mouse accuracy by creating a bunch of random circles which the player is meant to click in a given amount of time. At the end of the game, it should tell the player their score, and how many misclicks they had.
我一直在使用乌龟来尝试这样做,但我被卡住了:
I've been using turtle to try and do this, but I'm stuck:
import turtle
import random
t = turtle.Pen()
win = turtle.Screen()
win.bgcolor("lightgreen")
win.title("clicky")
def mycircle(red, green, blue):
t.color(red, green, blue)
t.begin_fill()
x = random.randint(10,50)
t.circle(x)
t.end_fill()
t.up()
y = random.randint(0,360)
t.seth(y)
if t.xcor() < -300 or t.xcor() > 300:
t.goto(0, 0)
elif t.ycor() < -300 or t.ycor() > 300:
t.goto(0, 0)
z = random.randint(0,100)
t.forward(z)
t.down()
for i in range(0, 20):
a = random.randint(0,100)/100.0
b = random.randint(0,100)/100.0
c = random.randint(0,100)/100.0
mycircle(a, b, c)
我一直试图弄清楚的主要问题是:
The main issues I've been trying to figure out are:
我怎样才能使圆圈彼此之间产生更远的距离?它们重叠很多时候,我希望避免这种情况.
How can I make the circles spawn further from each other? They overlapquite often and I want that to be avoided.
如何让圆圈立即生成而不是必须生成绘制?
How can I make the circles spawn instantly rather than having to bedrawn?
推荐答案
我们可以跟踪已经创建的圆,并确保它们的中心彼此相距至少一个直径.您当前的圆圈放置逻辑过于复杂且存在错误.让我们尝试简化它并确保在窗口内完全绘制圆圈.
We can keep track of circles already created and make sure their centers are at least a diameter away from each other. Your current circle placement logic is too complicated along with being faulty. Let's try to simplify it and make sure circles are drawn completely within the window.
如何让圆圈立即生成而不是必须生成画的?
我们可以戳它们而不是绘制它们.但是,由于您绘制的圆圈很少,我们可以使每个圆圈都成为乌龟.这使得确定您是否单击了一个圆圈并删除该圆圈变得更简单.我添加了代码,供您扩展,删除您点击的任何圆圈:
We could stamp them rather than draw them. However, since you are drawing so few circles, we can make every circle a turtle. This makes determining if you clicked on a circle, and removing that circle, simpler. I've added code, for you to expand on, that removes any circle that you click on:
from turtle import Turtle, Screen
from random import random, randint
CURSOR_SIZE = 20
def my_circle(color):
radius = randint(10, 50)
circle = Turtle('circle', visible=False)
circle.shapesize(radius / CURSOR_SIZE)
circle.color(color)
circle.penup()
while True:
nx = randint(2 * radius - width // 2, width // 2 - radius * 2)
ny = randint(2 * radius - height // 2, height // 2 - radius * 2)
circle.goto(nx, ny)
for other_radius, other_circle in circles:
if circle.distance(other_circle) < 2 * max(radius, other_radius):
break # too close, try again
else: # no break
break
circle.showturtle()
circle.onclick(lambda x, y, t=circle: t.hideturtle()) # expand this into a complete function
return radius, circle
screen = Screen()
screen.bgcolor("lightgreen")
screen.title("clicky")
width, height = screen.window_width(), screen.window_height()
circles = []
for _ in range(0, 20):
rgb = (random(), random(), random())
circles.append(my_circle(rgb))
screen.mainloop()
您需要解决的一个问题是确保您的圆圈颜色与您的背景颜色不太相似(或相同),否则您将寻找一个不可见的圆圈.此外,如果需要,我们还可以进一步加快圆的绘制过程.
One issue you need to work out is making sure your circle color isn't too similar to (or the same as) your background color, otherwise you'll be hunting an invisible circle. Also, we might be able to speed up the circle drawing process even more, if needed.
这篇关于随机定位的圆圈点击游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!