I am trying to blit an image on the screen when clicking a button in line
screen.blit(buttonPlusImage,
[random.randrange(560, 680),
random.randrange(330, 450)])
but the image disappears not long after because of the screen refreshes I got (I am using these 3 lines for updating the display: pygame.display.flip()
, FPS.tick(144)
and screen.fill(white)
). How can i do it in a way that the image stays on the screen and not go away with the screen.fill(white)? (I think that is what's causing it).
I pasted the code in https://dpaste.org/qGdi
import pygame
import random
pygame.init()
# variables
mainLoop = True
font = pygame.font.SysFont('comicsansms', 25)
white = [255, 255, 255]
green = [0, 150, 0]
gray = [140, 140, 140]
black = [0, 0, 0]
clickNum = 0
clickAmount = 1
FPS = pygame.time.Clock()
screen = pygame.display.set_mode((1300, 700))
# functions
def switchButton01():
global button02
button02 = pygame.transform.scale(button02, (100, 100))
screen.blit(button02, [580, 350])
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
global clickNum
clickNum += 1
screen.blit(buttonPlusImage, [random.randrange(560, 680), random.randrange(330, 450)])
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
# load images
button01 = pygame.image.load('button_100.png')
button02 = pygame.image.load('button_100hovered.png')
icon = pygame.image.load('icon_128.png')
buttonPlusImage = pygame.image.load('buttonPlus_32.png')
# window itself and icon
pygame.display.set_caption("incremental adventures")
pygame.display.set_icon(icon)
while mainLoop:
pygame.display.flip()
FPS.tick(144)
screen.fill(white)
# actual content in the game
button01 = pygame.transform.scale(button01, (100, 100))
screen.blit(button01, [580, 350])
click_counter_text = font.render("Click counter: ", True, black, white)
screen.blit(click_counter_text, [525, 50])
button01rect = button01.get_rect(center=(630, 400))
button01collidepoint = button01rect.collidepoint(pygame.mouse.get_pos())
click_counter = font.render((str(clickNum)), True, black, white)
screen.blit(click_counter, [700, 50])
if button01collidepoint:
switchButton01()
# quits
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
quit()