单击圆形游戏单击ci内外

2024-06-26 11:22:05 发布

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

我试着编写一个游戏,有一个红色的圆圈,其中用户应该点击多达7次的窗口。如果用户在圆外单击,圆将更改其位置到用户单击的位置。当用户在圆圈内点击3次(不必排成一行)或总共点击7次时,游戏应该结束。 我已经编码并完成了大部分工作,我想,只是我似乎不能让它像我想的那样工作。你知道吗

from graphics import *

def draw_circle(win, c=None):

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

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

def main():
    win= GraphWin("game",800,800)
    score = 0
    var,x,y = draw_circle(win)
    while score <= 7:
        mouseClick2=win.getMouse()
        if mouseClick2.y >= y-50 and mouseClick2.y <= y +50 and 
mouseClick2.x >= x-50 and mouseClick2.x <= x+50:
            score=score + random.randint(0,5)
        var,x,y = draw_circle(win, c)

    print ("Success!")
    print (("the score is, {0}").format(score))

提前谢谢你的帮助!你知道吗


Tags: and用户游戏vardefrandomwinscore
2条回答

我不是一个真正的Python,但我看到你的杀手锏是错误的。如果还有其他问题,请给我留言。你知道吗

解决要循环的hitbox:

你已经写的东西是好东西,但你应该检查一下,如果点击是在圆圈不是正方形。毕达哥拉斯三角形就是解决这个问题的方法。 支票:

if (math.sqrt(delta_x **2 + delta_y **2) <= circle_radius)

其中delta_x和delta_y是中心坐标减去鼠标位置

我看到一些问题。你知道吗

  • 您的if mouseClick2.y >= y-50...条件分布在两行上,但没有行连续字符。你知道吗
  • 你从不打电话给main()。你知道吗
  • 不导入random。你知道吗
  • 使用参数c调用while循环中的draw_circle,但是全局范围中没有该名称的变量。你可能是想通过var。你知道吗
  • ^draw_circle中的{}表面上是指要操纵的圆对象,但有一半时间是操纵var而不是c。你知道吗
  • 给循环中的cvar赋值,但从不使用它。你知道吗
  • draw_circle中的else块通过从c.p1中减去光标坐标来计算移动增量。但c.p1是圆的左上角,而不是圆心。所以你的命中检测减少了50个像素。你知道吗

你知道吗

import random
from graphics import *

def draw_circle(win, c=None):

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

    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 main():
    win= GraphWin("game",800,800)
    score = 0
    var,x,y = draw_circle(win)
    while score <= 7:
        mouseClick2=win.getMouse()
        if mouseClick2.y >= y-50 and mouseClick2.y <= y +50 and \
mouseClick2.x >= x-50 and mouseClick2.x <= x+50:
            score=score + random.randint(0,5)
        var,x,y = draw_circle(win, var)

    print ("Success!")
    print (("the score is, {0}").format(score))

main()

其他可能的改进:

  • 点击检测将检查光标是否位于以圆为中心的50x50矩形中。如果测量光标与圆心之间的距离并检查其是否小于半径,则可以改为检查光标是否在圆内。你知道吗
  • varc可以有更多的描述性名称。你知道吗
  • mouseClick2作为一个名字没有多大意义,因为没有mouseClick1。你知道吗
  • 移动增量算法可以简化:(a-b) * -1(b-a)相同。你知道吗
  • 如果只使用一次变量的值,那么如果嵌套表达式,有时完全可以避免创建变量。你知道吗
  • 最好是定义常量,例如圆的半径,而不是在代码中使用幻数。你知道吗
  • 通过使用+=增加分数,可以保存五个字符。你知道吗

你知道吗

import math
import random
from graphics import *

RADIUS = 50

def draw_circle(win, circle=None):

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

    if circle is None:
        circle = Circle(Point(x,y),RADIUS)
        circle.setFill(color_rgb(200,0,0))
        circle.draw(win)
    else:
        circle.move(
            x - circle.p1.x - RADIUS, 
            y - circle.p1.y - RADIUS
        )
    return (circle, x, y)

def main():
    win= GraphWin("game",800,800)
    score = 0
    circle,x,y = draw_circle(win)
    while score <= 7:
        cursor = win.getMouse()
        if math.hypot(cursor.x - x, cursor.y - y) <= RADIUS:
            score += random.randint(0,5)
        circle,x,y = draw_circle(win, circle)

    print ("Success!")
    print (("the score is, {0}").format(score))

main()

相关问题 更多 >