pygame - 如何根据掷骰子使圆圈移动? [英] pygame - How to make a circle move based on a dice roll?

查看:60
本文介绍了pygame - 如何根据掷骰子使圆圈移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据 1-5 的骰子掷出一个圆圈来移动.

I'm trying to get a circle to move based on a dice roll of 1-5.

例如.如果它滚动 4,它会将圆圈移动到总共 580 - (55 * 4) 个像素.

Eg. if it rolls a 4, it will move the circle to a total of 580 - (55 * 4) pixels.

但是我做不到,所以圆圈基本上会动态清除自己的旧版本,有什么想法吗?

However I can't make it so the circle will basically clear the old version of itself dynamically, any ideas?

此外,我正在努力使提示消息在用户看到骰子后清除自己所掷骰子上的数字.所以就像按下一个按钮让程序知道你已经看到骰子编号并希望继续,但是我不能在不清除整个游戏窗口的情况下做到这一点,当游戏窗口清除时,它会忘记旧的圆坐标使计数器再次回到起始位置...

Also, I am trying to make it so the prompt message saying what number on the dice you have rolled clear itself after the user has seen it. so like pressing a button to make the program know that you have seen the dice number and wishes to proceed, however I can't do that without clearing the entire game window and when the game window clears, it will forget the old circle coordinates which makes the counters move back to the starting position again...

我按照要求尝试的 -

What I've attempted as requested -

while True:
    for event in pygame.event.get():
        if event.type==pygame.KEYDOWN and event.key==pygame.K_SPACE:

            diceRoll = random.randint(1,5)
            diceAlert = font.render("You rolled a: "+str(diceRoll), True, red)
            moveCounter = font.render("Please click 1 or 2 to select the counter", True, red)

            screen.blit(diceAlert, [665, 35])
            screen.blit(moveCounter, [665, 70])
            pygame.display.update()


        if event.type==pygame.KEYDOWN and event.key==pygame.K_1:

            if diceRoll == 1:
                cover = int(0)
            if diceRoll == 2:
                cover = int(2)
            if diceRoll == 3:
                cover = int(2)
            if diceRoll == 4:
                cover = int(3)

            counter1 = pygame.draw.circle(screen, white, (250, 580-(cover*55)), 15, 0)

            counter1 = pygame.draw.circle(screen, red, (250, var[0]-55*diceRoll), 15, 0)

            window()

            pygame.display.update()

推荐答案

您似乎只有在按下按钮时才会闪烁圆圈.这迫使您使用屏幕作为您游戏状态的唯一信息.相反,您应该将这两个分开.

You seem to be bliting a circle only if a button has been pressed. This forces you to use your screen, as your only information of the state of your game. Instead you should separate those 2.

这是一个 pygame 游戏的模板:

Here is a template of a pygame game:

init()
while not finished:
    draw()
    update()
    input()

这 4 个函数做不同的事情.您不应该在输入法中进行任何绘图.

These 4 functions do different things. You should not do any drawing in the input method.

让我们来看看这些函数:

Let's go through these functions:

init() 初始化 pygame、屏幕、加载文件并创建任何需要的实例.在您的情况下,您可以创建一个变量来存储当前的圆位置.

init() initializes pygame, screen, loads files, and creates any instances that are needed. In your case here you can create a variable that will store the current circle position.

draw() 使用fill() 清除屏幕,并在屏幕上绘制所有精灵.在你的情况下,他会选择圆圈位置,并相应地绘制它.

draw() clears the screen with fill(), and draws all the sprites on screen. In your case, his will the the circle position, and draw it accordingly.

update() 这会更新屏幕上的精灵,检查碰撞等.你没有任何东西会使用这个函数,因为你的游戏是静态的,它不会及时改变.

update() this updates sprites on the screen, checks collisions etc. You don't have anything that will use this function, since your game is static, it does not change in time.

input() 接受输入,并改变对象的状态.在您的情况下,您将测试是否按下了按钮,如果按下,则更改圆圈位置.

input() takes input, and changes the state of objects. In your case, you would test to see if a button was pressed, and if so, change the circle position.

整理代码后,它看起来像这样:

After tidying up your code it will look like this:

circle_pos = (0,0)
circle_pos2 = (0,0)
while True:
    screen.blit(diceAlert, [665, 35])
    screen.blit(moveCounter, [665, 70])            
    counter1 = pygame.draw.circle(screen, white, circle_pos, 15, 0)
    counter1 = pygame.draw.circle(screen, red, circle_pos2, 15, 0)

    pygame.display.update()

    for event in pygame.event.get():
        if event.type==pygame.KEYDOWN and event.key==pygame.K_SPACE:

            diceRoll = random.randint(1,5)
            diceAlert = font.render("You rolled a: "+str(diceRoll), True, red)
            moveCounter = font.render("Please click 1 or 2 to select the counter", True, red)

        if event.type==pygame.KEYDOWN and event.key==pygame.K_1:

            if diceRoll == 1:
                cover = int(0)
            if diceRoll in (2,3):
                cover = int(2)
            if diceRoll == 4:
                cover = int(3)
            circle_pos = (250, 580-(cover*55))

代码功能不全,因为我不知道您要完成什么.我希望这个模板能帮助你前进.

The code is not fully functional, since I did not know what you are trying to accomplish. I hope this template will get you going.

这篇关于pygame - 如何根据掷骰子使圆圈移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆