【问题标题】:How do i make my wheel of fortune spin slow in Processing (with python)我如何使我的命运之轮在处理中旋转缓慢(使用python)
【发布时间】:2020-11-22 15:07:01
【问题描述】:

我有一个学校项目,我必须在处理 3.5.4 时使用 python 制作应用程序, 现在经过多次尝试,我创造了一个命运之轮,在鼠标单击中间按钮后可以旋转和停止。当按下“停止”按钮并最终停止时,我基本上想让我的命运之轮旋转得更慢,我尝试使用像“loop_number”这样的变量来检查,如果它是一个低循环数,速度轮子掉下来了,但这似乎对我不起作用,因为处理是内置在一个大的while循环中的,它会在循环开始时再次“刷新”所有变量。无论如何,这是我的代码(我知道它并不完美,我还在学习)。

import math
from time import sleep

totalWidth = 1000
totalHeight = 1000
circleX = totalWidth // 2
circleY = totalHeight // 2
circleDist = 0  # this will be defined outside of the functions so we can use this variable globally
outerCircleRadius = (circleX + circleY) // 2 - 60
innerCircleRadius = 75
line_points = {}  # used to store the values of all the line points
turningWheel = False  # used to determine if the wheel has to be turning or not
all_chances = ['Je vind 25 FashionPoints op straat!', 'Je verliest 30 FashionPoints!', 'Je wint een Ov-Kaart van 3 stations!',
               'Draai het geluksrad nogmaals!', 'Je verliest je duurste schoen!', 'Je wint 20 Fashionpoints!', 
               'Je goedkoopste shirt word beroofd!', '    Betaal een boete van 20 FashionPoints', 'Je wint 1x erg dure kleding']
degree_points = [0, 360]  # this will be used to position the lines along the outside of the circle
velocity = [0, 0]  # this will be used to determine how fast the circle turns

def setup():
    size(totalWidth, totalHeight)
    ellipseMode(RADIUS) # the ellipses get made through their radiuses and not x and y
    
    
def draw():
    global number
    global circleDist
    global velocity
    global degree_points
    
    background(255, 255, 255)
    strokeWeight(2)
    
    circleDist = dist(mouseX, mouseY, circleX, circleY)  # the distance of the middle to the mouse, this will be used later with the inner circle
    
    # the black line of the outer circle
    fill(0, 0, 0)
    ellipse(circleX, circleY,  # the middle of the created circle
            outerCircleRadius + 20, outerCircleRadius + 20)
    
    # outer circle; TODO: change the color
    fill(255, 255, 255)
    ellipse(circleX, circleY,  # the middle of the created circle
            outerCircleRadius, outerCircleRadius)
    
    # IMPORTANT: this section covers drawing in the circle with the triangles(lines) and chances
    # the cosin and sin variable are here to help with finding the right coordinates
    # along the line of the circle
    if turningWheel:
        degree_points[0] += velocity[0]
        degree_points[1] += velocity[1]
        
    for index, angle in enumerate(range(degree_points[0], degree_points[1], 40)):
        # the first two coordinates will help to calculate how far the point is from the center of the circle
        x1 = outerCircleRadius * cos(angle * math.pi / 180)
        y1 = outerCircleRadius * sin(angle * math.pi / 180)
        x2 = circleX + x1
        y2 = circleY + y1

        # line function works like this: line(beginX, beginY, endX, endY)
        textAlign(CENTER, LEFT)
        line(circleX, circleY, x2, y2)
        line_points.setdefault('line' + str(index), [x2, y2])  # adding the line point to a dictionary
        # getting the text in the right place
        
        # this section covers centering the chance text in the middle of each triangle
        textSize(16)
        middle_x = circleX + outerCircleRadius * cos((angle + 20) * math.pi / 180)
        middle_y = circleY + outerCircleRadius * sin((angle + 20) * math.pi / 180)
        textX = circleX + ((middle_x - circleX) / 2)
        textY = circleY + ((middle_y - circleY) / 2)
        pushMatrix()  # opens a change for the matrix (recentering the matrix for the rotation)
        translate(textX, textY)  # sets the new center for rotation
        fill(255, 0, 0)
        rotate((angle + 20) * math.pi / 180)
        text(all_chances[index], 0, 0)
        popMatrix()  # closes the change for the matrix
    
    # inner circle; TODO: this has to be filled in last
    # otherwise the chances will overlap this circle
    fill(255, 0, 0)
    ellipse(circleX, circleY, innerCircleRadius, innerCircleRadius)
    
    # text inside the inner circle
    textSize(35)
    fill(255, 255, 255)
    textAlign(CENTER, CENTER)
    if turningWheel:
        text('STOP', circleX, circleY)
    else:
        text('START', circleX, circleY)
    
    # triangle on the side of the circle; this has to be filled in last,
    # after all the chances have been filled in, otherwise this gets overlapped, 
    fill(255, 0, 0)
    triangle(totalWidth - 115, totalHeight // 2,  # tip of the triangle pointing towards the middle
        totalWidth - 10, totalHeight // 2 + 50,  # uppermost corner of the triangle
        totalWidth - 10, totalHeight // 2 - 50)  # lowermost corner of the triangle
    
def mousePressed():
    global turningWheel
    global degree_points
    global velocity

    if circleDist < innerCircleRadius:
        turningWheel = not turningWheel

    if turningWheel:
        velocity = [4, 4]

【问题讨论】:

    标签: python processing


    【解决方案1】:

    如果这段代码对你有意义,我使用一个名为 stop 的布尔标志,如果你用鼠标中键单击(或者如果你按下空格键),它将变为 True,这将触发每帧减少 %5 的 angular_vel(如 angular_vel = angular_vel * 0.95)直到它接近于零(angular_vel &lt; 0.1),然后它将被设置为 0 并且 stop 标志将被设置回 @ 987654330@.

    stop = False
    angular_vel = 3  # degrees per frame
    angle = 0  # degrees
    
    def setup():
        size(400, 400)
        frameRate(20) # slower frame rate
        
    def draw():
        global angle, angular_vel, stop
        background(128)
        fill(255)
        circle(200, 200, 400)
        translate(200, 200)
        rotate(radians(angle))
        if angular_vel == 0:
            fill(255, 0, 0)
        else:
            fill(0)
        circle(180, 0, 20)
        
        angle += angular_vel
        if stop:
            angular_vel *= 0.95
        if angular_vel < 0.1:
            stop = False
            angular_vel = 0
    
    def mousePressed():
        global stop
        if mouseButton == CENTER:
            stop = True
    
    def keyPressed():
        global stop, angular_vel
        if key == " ":
            stop = True
        if key == "r":    # make it spin again
            angular_vel = 3
    

    PS:注意 Processing 给你一个PI 常量,不需要导入math

    【讨论】:

      猜你喜欢
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多