获取用户单击的圆位置?

2024-09-27 18:22:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的代码,这应该是一个圆圈游戏,如果用户点击圈内2次,游戏将结束。或在圆圈外(总共点击7次,游戏也将结束),圆圈将改变其位置。这是我需要一些指导和解释如何解决

当我在圆圈外单击时,我需要它出现在我单击的位置上,而不是像现在这样只是窗口中的一个随机位置。 请解释一下我是如何做到这一点的:)

import random
from graphics import *

def draw_circle(win, c = None):

    x = random.randint(0,600)
    y = random.randint(0,600)

    if c is None:
        centa = Point(x,y)
        c = Circle(centa,50)
        c.setFill(color_rgb(200,0,0))
        c.draw(win)
    else:
        center_x = c.p1.x + 50
        center_y = c.p1.y + 50
        x_dif = (center_x - x) * -1
        y_dif = (center_y - y) * -1
        c.move(x_dif, y_dif)
    return (c, x, y)

def circleGame():
    win= GraphWin("Click circles", 600, 600)
    totalClicks = 0
    var,x,y = draw_circle(win)
    clickInside = 0

    while totalClicks < 10 and clickInside < 3:
        totalClicks += 1
        mouseClick2 = win.getMouse()
        if mouseClick2.y >= y-50 and mouseClick2.y <= y +50 and \
mouseClick2.x >= x-50 and mouseClick2.x <= x+50:
            clickInside +=1
            totalClicks += random.randint(0,5)
        var,x,y = draw_circle(win, var)

circleGame()

提前多谢了


Tags: andimport游戏varrandomwincenterdraw
2条回答

draw_circle函数中,您将xy设置为随机值,然后执行x_dif =(center_x - x) * -1y_dif = (center_y - y) * -1,因此现在您将基于随机值xy来确定位置。然后返回这些随机值。而且,x - center_x(center_x - x) * -1简单

另一个问题是if mouseClick2.y >= y-50 and mouseClick2.y <= y +50 and mouseClick2.x >= x-50 and mouseClick2.x <= x+50:。这不仅比你需要的复杂,而且不是一个圆的测试;这是对正方形的测试。你应该有(mouseClick2.y-y)**2+(mouseClick2.x-x)**2 <= 2500

你的draw_circle函数在做两件独立的事情,所以我不确定把它作为一个单独的函数是否有意义。它没有正确更新圆的x和y坐标,因为你总是返回随机坐标,即使你应该返回移动圆的坐标。在下面的代码中,我将该函数的两部分分解为主circle_game函数

我已经把你在正方形内点击的检查换成了在圆形内点击的检查:(mouse.x - circle_x ) ** 2 + (mouse.y - circle_y) ** 2 <= CIRCLE_RADIUS_SQUARED

我也不知道totalClicks += random.randint(0,5)想要达到什么目的

import random

from graphics import *


def circle_game():
    CANVAS_WIDTH = 600
    CANVAS_HEIGHT = 600
    CIRCLE_RADIUS = 50
    CIRCLE_RADIUS_SQUARED = CIRCLE_RADIUS * CIRCLE_RADIUS

    window = GraphWin("Click circles", CANVAS_WIDTH, CANVAS_HEIGHT)

    # Draw circle at random location to start game
    circle_x = random.randint(0, CANVAS_WIDTH)
    circle_y = random.randint(0, CANVAS_HEIGHT)
    circle_point = Point(circle_x, circle_y)
    circle = Circle(circle_point, CIRCLE_RADIUS)
    circle.setFill(color_rgb(200, 0, 0))
    circle.draw(window)

    total_clicks = 0
    inside_clicks = 0
    while total_clicks < 10 and inside_clicks < 3:
        mouse = window.getMouse()
        total_clicks += 1
        if (mouse.x - circle_x ) ** 2 + (mouse.y - circle_y) ** 2 <= CIRCLE_RADIUS_SQUARED:
            # Click was inside circle so we don't move circle
            inside_clicks +=1
        else:
            # Click was not inside circle so we move circle to mouse location
            dx = mouse.x - circle_x
            dy = mouse.y - circle_y
            circle.move(dx, dy)
            circle_x += dx
            circle_y += dy


if __name__ == '__main__':
    circle_game()

相关问题 更多 >

    热门问题