'How to make a new image appear when a button is pressed Pygame
I want to make a game where if you press a button, an image will pop up, but I can't figure it out how to do it. Maybe I'm putting it in with a plus indent, or messing up something, I have no clue on what I'm doing wrong here. Any help would be appreciated! Anyways, here's the code: (Important part is the number_one function)
import pygame
import os
import random
WIDTH, HEIGHT = (1024, 768)
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Joe mama")
number = [0, 1, 2]
# Button class
class Button():
def __init__(self, x, y, image):
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
def draw(self):
#Draw button on screen
WIN.blit(self.image, (self.rect.x, self.rect.y))
def number_one():
# Picture Variables
kerdes1 = pygame.image.load("Zene/1/Kérdés-1.png")
A_select = pygame.image.load("Zene/1/A megjelölve.png")
B_select = pygame.image.load("Zene/1/B megjelölve.png")
C_select = pygame.image.load("Zene/1/C megjelölve.png")
D_select = pygame.image.load("Zene/1/D megjelölve.png")
WIN.blit(kerdes1, (0, 0))
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
WIN.blit(A_select, (0, 0))
print("pressed up")
def number_two():
kerdes2 = pygame.image.load("Zene/2/Kérdés-2.png")
WIN.blit(kerdes2, (0, 0))
while len(number) > 0:
if len(number) == 0:
break
result = random.choice(number)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if result == 1:
print("1")
number.remove(1)
number_one()
print(number)
pygame.display.update()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
