I have a pygame project where I need to display a button according to a randomly generated sequence. It is currently just exiting as soon as I start it
import pygame
import os
import random
# Initialize pygame starts
pygame.init()
# Set the dimensions of the screen
screen_width = 1275
screen_height = 750
# Set the colors
white = (255, 255, 255)
black = (0, 0, 0)
# Set the font
font = pygame.font.SysFont('Arial', 20)
# Set the button properties
button_radius = 70
button_x_spacing = 400
button_y_spacing = 200
# Set the flower image
flower_image = pygame.image.load(os.path.join(os.path.expanduser("~"), "Documents", "flower.jpg"))
flower_rect = flower_image.get_rect()
flower_rect.center = (screen_width//2, screen_height//2)
# Set the button positions
button_positions = [
(button_x_spacing, button_y_spacing),
(button_x_spacing, button_y_spacing + 200),
(button_x_spacing, button_y_spacing + 400)
]
# Initialize the screen
screen = pygame.display.set_mode((screen_width, screen_height))
# Set the caption
pygame.display.set_caption("Marmoset Buttons")
# Set the clock
clock = pygame.time.Clock()
# Set the loop variable
running = True
# Set the timer
timer = 0
# Draw the buttons on the screen
#def draw_buttons():
#for i, pos in enumerate(button_positions):
#for i in sequence:
#pygame.draw.circle(screen, white, button_positions[i], button_radius)
#text_surface = font.render(f"Button {i}", True, black)
#text_rect = text_surface.get_rect(center=button_positions[i])
#screen.blit(text_surface, text_rect)
# Generating sequence of numbers
sequence = []
for i in range(0,10):
n = random.randint(0,2)
sequence.append(n)
# Main game loop
#while running:
# Set the frame rate
#clock.tick(60)
for i in sequence:
# Check for events
screen.fill(black)
pygame.draw.circle(screen, white, button_positions[i], button_radius)
text_surface = font.render(f"Button {i}", True, black)
text_rect = text_surface.get_rect(center=button_positions[i])
screen.blit(text_surface, text_rect)
pygame.display.update()
for event in pygame.event.get():
#if event.type == pygame.QUIT:
#running = 1
if event.type == pygame.MOUSEBUTTONDOWN:
distance_button = pygame.math.Vector2(button_positions[i]) - pygame.math.Vector2(pygame.mouse.get_pos())
flower_image = pygame.image.load(os.path.join(os.path.expanduser("~"), "Documents", f"flower{i}.jpg"))
#distance_button2 = pygame.math.Vector2(button_positions[1]) - pygame.math.Vector2(pygame.mouse.get_pos())
#distance_button3 = pygame.math.Vector2(button_positions[2]) - pygame.math.Vector2(pygame.mouse.get_pos())
if distance_button.length() < button_radius:
screen.blit(flower_image, flower_rect)
#timer = pygame.time.get_ticks()
#flower_image = pygame.image.load(os.path.join(os.path.expanduser("~"), "Documents", "flower.jpg"))
#elif distance_button2.length() < button_radius:
#timer = pygame.time.get_ticks()
#flower_image = pygame.image.load(os.path.join(os.path.expanduser("~"), "Documents", "flower1.jpg"))
#elif distance_button3.length() < button_radius:
#timer = pygame.time.get_ticks()
#flower_image = pygame.image.load(os.path.join(os.path.expanduser("~"), "Documents", "flower2.jpg"))
#else:
#timer = pygame.time.get_ticks()
#flower_image = pygame.image.load(os.path.join(os.path.expanduser("~"), "Documents", "flower3.jpg"))
# Fill the screen with black
#screen.fill(black)
# Draw the buttons on the screen
#draw_buttons()
# Check the timer
#if timer != 0:
#current_time = pygame.time.get_ticks()
#if current_time - timer < 3000:
#screen.blit(flower_image, flower_rect)
#else:
#timer = 0
# Update the screen
#pygame.display.update()
# Quit pygame
#pygame.quit()
I tried commenting some previous unnecessary code I had but nothing is changing. I had it working before where I displayed all 3 buttons at once but when I tried changing it to randomly show one it didn't work.
Aucun commentaire:
Enregistrer un commentaire