首页 > 解决方案 > 需要在处理中进行动画骰子滚动

问题描述

我对这一切都是新手,我需要为学校作业制作一个动画骰子。真的不知道该怎么做。

到目前为止,我设法得到了一个工作骰子。但是当我按下滚动按钮时,我首先需要有一个动画才能看到结果。现在,当我单击时,我会立即看到结果。

关于如何做到这一点的任何提示或帮助?

到目前为止我的代码:

def upperText():
    fill(0, 0, 0) #text color (black)
    textSize(45)
    textAlign(CENTER)
    text('Gooi de linker of rechter dobbelsteen', width/2, 175)
    textSize(25)
    text('De linker dobbelsteen heeft 3 ogen en de rechter dobbelsteen heeft 6 ogen', width/2, 275)

def rollButtons(): #the lower two buttons
#Left button
    fill(255, 255, 255)
    rectMode(CENTER)
    rect(width/4, 1000, 350, 200)
    fill(0, 0, 0)
    textSize(40)
    textAlign(CENTER)
    text('Gooi!', width/4, 1010)
#Right button
    fill(255, 255, 255)
    rectMode(CENTER)
    rect((width/4)*3, 1000, 350, 200)
    fill(0, 0, 0)
    textSize(40)
    textAlign(CENTER)
    text('Gooi!', (width/4)*3, 1010)

def leftDice(): #the left dice
    fill(255, 255, 255)
    rectMode(CENTER)
    rect(width/4, 600, 500, 500)
    global img3
    img3 = loadImage("dice3.png")
    imageMode(CENTER)
    image(img3, width/4, 600, 490, 490)

def rightDice(): #the right dice
    fill(255, 255, 255)
    rectMode(CENTER)
    rect((width/4)*3, 600, 500, 500)
    global img6
    img6 = loadImage("dice6.png")
    imageMode(CENTER)
    image(img6, (width/4)*3, 600, 490, 490)

def rollLeftDice(): #fuction that runs when the left 'Gooi!' button is pressed
    #Roll calculations:
        #3/6 chance to roll 1
        #2/6 chance to roll 2
        #1/6 chance to roll 3
        #therefore I used random(1, 7) while I'm only showing the pictures from 1 to 3.
    rolledLeftDice = int(random(1, 7))
    if rolledLeftDice == 1:                       #if random=1 you rolled 1
        fill(255, 255, 255)
        rectMode(CENTER)
        rect(width/4, 600, 500, 500)
        global img1
        img1 = loadImage("dice1.png")
        imageMode(CENTER)
        image(img1, width/4, 600, 490, 490)
    if rolledLeftDice == 2:                       #if random=2 you rolled 1
        fill(255, 255, 255)
        rectMode(CENTER)
        rect(width/4, 600, 500, 500)
        global img1
        img1 = loadImage("dice1.png")
        imageMode(CENTER)
        image(img1, width/4, 600, 490, 490)
    if rolledLeftDice == 3:                       #if random=3 you rolled 1
        fill(255, 255, 255)
        rectMode(CENTER)
        rect(width/4, 600, 500, 500)
        global img1
        img1 = loadImage("dice1.png")
        imageMode(CENTER)
        image(img1, width/4, 600, 490, 490)
    if rolledLeftDice == 4:                       #if random=4 you rolled 2
        fill(255, 255, 255)
        rectMode(CENTER)
        rect(width/4, 600, 500, 500)
        global img2
        img2 = loadImage("dice2.png")
        imageMode(CENTER)
        image(img2, width/4, 600, 490, 490)
    if rolledLeftDice == 5:                       #if random=5 you rolled 2
        fill(255, 255, 255)
        rectMode(CENTER)
        rect(width/4, 600, 500, 500)
        global img2
        img2 = loadImage("dice2.png")
        imageMode(CENTER)
        image(img2, width/4, 600, 490, 490)
    if rolledLeftDice == 6:                       #if random=6 you rolled 3
        fill(255, 255, 255)
        rectMode(CENTER)
        rect(width/4, 600, 500, 500)
        global img3
        img3 = loadImage("dice3.png")
        imageMode(CENTER)
        image(img3, width/4, 600, 490, 490)

def rollRightDice(): #fuction that runs when the right 'Gooi!' button is pressed
    rolledRightDice = int(random(1, 7))
    if rolledRightDice == 1:
        fill(255, 255, 255)
        rectMode(CENTER)
        rect((width/4)*3, 600, 500, 500)
        global img1
        img1 = loadImage("dice1.png")
        imageMode(CENTER)
        image(img1, (width/4)*3, 600, 490, 490)
    if rolledRightDice == 2:
        fill(255, 255, 255)
        rectMode(CENTER)
        rect((width/4)*3, 600, 500, 500)
        global img2
        img2 = loadImage("dice2.png")
        imageMode(CENTER)
        image(img2, (width/4)*3, 600, 490, 490)
    if rolledRightDice == 3:
        fill(255, 255, 255)
        rectMode(CENTER)
        rect((width/4)*3, 600, 500, 500)
        global img3
        img3 = loadImage("dice3.png")
        imageMode(CENTER)
        image(img3, (width/4)*3, 600, 490, 490)
    if rolledRightDice == 4:
        fill(255, 255, 255)
        rectMode(CENTER)
        rect((width/4)*3, 600, 500, 500)
        global img4
        img4 = loadImage("dice4.png")
        imageMode(CENTER)
        image(img4, (width/4)*3, 600, 490, 490)
    if rolledRightDice == 5:
        fill(255, 255, 255)
        rectMode(CENTER)
        rect((width/4)*3, 600, 500, 500)
        global img5
        img5 = loadImage("dice5.png")
        imageMode(CENTER)
        image(img5, (width/4)*3, 600, 490, 490)
    if rolledRightDice == 6:
        fill(255, 255, 255)
        rectMode(CENTER)
        rect((width/4)*3, 600, 500, 500)
        global img6
        img6 = loadImage("dice6.png")
        imageMode(CENTER)
        image(img6, (width/4)*3, 600, 490, 490)

def setup():
    size(1200, 1200)
    background(25, 255, 0)
    leftDice()
    rightDice()

def draw():
    upperText()
    rollButtons()

def MouseInSpace(x, y, w, h):
    if (mouseX > x) and (mouseX < x+w) and (mouseY > y) and (mouseY < y+h): 
        return True
    else:
        return False

def mousePressed():
    if MouseInSpace(125, 900, 350, 200): #left button coordinates
        rollLeftDice()
    if MouseInSpace(725, 900, 350, 200): #right button coordinates
        rollRightDice()

标签: pythonanimationprocessingdice

解决方案


推荐阅读