Reputation: 337
I'm trying to get a circle to move based on a dice roll of 1-5.
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()
Upvotes: 0
Views: 1410
Reputation: 11170
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.
Here is a template of a pygame game:
init()
while not finished:
draw()
update()
input()
These 4 functions do different things. You should not do any drawing in the input method.
Let's go through these functions:
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()
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()
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()
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.
Upvotes: 4