surface_obj.fill('color') function is causing error
Question:
i am trying to make hangman with pygame
1 > if you copy following code in IDE and run it will prompt pygame window if you clicked inside button(circles) it will basically disappear . Like in typical hangman game
2> BUT if you comment line < win.fill(‘darkred’)> then code will stop working , meaning button(circles) will no more disappear
** can anyone tell why? **
import pygame , math
pygame.init()
WIDTH , HEIGHT = 900 , 600
win = pygame.display.set_mode((WIDTH , HEIGHT))
pygame.display.set_caption('Hangmane')
RUN = True
# indexing button coordinates and appending in letters(list)
letters = [ ]
RADIUS = 20
BUTTON_X = 0
BUTTON_Y = 457
for i in range(26):
BUTTON_X += 60
if i == 14:
BUTTON_X = 100
BUTTON_Y = 520
#pygame.draw.circle(win,'green',(BUTTON_X,BUTTON_Y),RADIUS,3)
letters.append([BUTTON_X, BUTTON_Y])
#drawing button(circle) in pygame window
def button():
for letter in letters:
BUTTON_X , BUTTON_Y = letter
pygame.draw.circle(win,'green',(BUTTON_X,BUTTON_Y),RADIUS,3)
# this will check and remove if button(circle) is clicked
def click_checker(coordinates):
letters.remove(coordinates)
while RUN:
win.fill('darkred') #if you comment this than code will not work as intended
button()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
RUN = False
if event.type == pygame.MOUSEBUTTONDOWN:
pos_x , pos_y = pygame.mouse.get_pos()
print(pos_x , pos_y)
for letter in letters:
BUTTON_X , BUTTON_Y = letter
dis = math.sqrt((BUTTON_X-pos_x)**2 + (BUTTON_Y-pos_y)**2)
if dis < RADIUS: # this will check if distance between mouse Cursor is less than button(circle) radius
#print('clicked')
click_checker(letter)
pygame.quit()
Answers:
You should first understand how the code works inside the while RUN:
loop:
- The window is filled with dark red erasing any previous buttons, and green buttons are drawn using the x and y coordinates in the
letters
list.
- On every press, if a button is pressed, then the button’s x and y coordinates are removed from the
letters
list.
- If button is not pressed, then nothing is removed from the
letters
list.
- Now, go to step 1 again.
If you comment out win.fill('darkred')
then the window will not be painted over, hence the previous buttons are not erased. So even though it will only redraw 25 buttons, the other button is still visible from the previous drawing.
i am trying to make hangman with pygame
1 > if you copy following code in IDE and run it will prompt pygame window if you clicked inside button(circles) it will basically disappear . Like in typical hangman game
2> BUT if you comment line < win.fill(‘darkred’)> then code will stop working , meaning button(circles) will no more disappear
** can anyone tell why? **
import pygame , math
pygame.init()
WIDTH , HEIGHT = 900 , 600
win = pygame.display.set_mode((WIDTH , HEIGHT))
pygame.display.set_caption('Hangmane')
RUN = True
# indexing button coordinates and appending in letters(list)
letters = [ ]
RADIUS = 20
BUTTON_X = 0
BUTTON_Y = 457
for i in range(26):
BUTTON_X += 60
if i == 14:
BUTTON_X = 100
BUTTON_Y = 520
#pygame.draw.circle(win,'green',(BUTTON_X,BUTTON_Y),RADIUS,3)
letters.append([BUTTON_X, BUTTON_Y])
#drawing button(circle) in pygame window
def button():
for letter in letters:
BUTTON_X , BUTTON_Y = letter
pygame.draw.circle(win,'green',(BUTTON_X,BUTTON_Y),RADIUS,3)
# this will check and remove if button(circle) is clicked
def click_checker(coordinates):
letters.remove(coordinates)
while RUN:
win.fill('darkred') #if you comment this than code will not work as intended
button()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
RUN = False
if event.type == pygame.MOUSEBUTTONDOWN:
pos_x , pos_y = pygame.mouse.get_pos()
print(pos_x , pos_y)
for letter in letters:
BUTTON_X , BUTTON_Y = letter
dis = math.sqrt((BUTTON_X-pos_x)**2 + (BUTTON_Y-pos_y)**2)
if dis < RADIUS: # this will check if distance between mouse Cursor is less than button(circle) radius
#print('clicked')
click_checker(letter)
pygame.quit()
You should first understand how the code works inside the while RUN:
loop:
- The window is filled with dark red erasing any previous buttons, and green buttons are drawn using the x and y coordinates in the
letters
list. - On every press, if a button is pressed, then the button’s x and y coordinates are removed from the
letters
list. - If button is not pressed, then nothing is removed from the
letters
list. - Now, go to step 1 again.
If you comment out win.fill('darkred')
then the window will not be painted over, hence the previous buttons are not erased. So even though it will only redraw 25 buttons, the other button is still visible from the previous drawing.