Untitled
unknown
aql
a year ago
1.8 kB
2
Indexable
import pygame import sys from pygame.locals import * import time # Initialize pygame pygame.init() # Constants for screen dimensions and colors SCREEN_WIDTH, SCREEN_HEIGHT = 640, 480 BG_COLOR = (255, 255, 255) # White background HOUSE_COLOR = (124, 252, 0) # Green house NIGHT_COLOR = (0, 0, 0) # Night is black BUILD_TIME = 600 # 10 minutes in seconds # Set up the display screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('Build a House Game') # Function to draw the house on the screen def build_house(): house_rect = pygame.Rect(SCREEN_WIDTH // 4, SCREEN_HEIGHT // 4, SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2) pygame.draw.rect(screen, HOUSE_COLOR, house_rect) # Main game loop def game_loop(): start_time = time.time() # Record the start time house_built = False # Flag to check if the house is built while True: # Game loop screen.fill(BG_COLOR) # Fill the screen with background color # Event handling for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_SPACE: house_built = True # Set the flag to True when spacebar is pressed # Game logic if house_built: build_house() # Draw the house if built elif time.time() - start_time >= BUILD_TIME: screen.fill(NIGHT_COLOR) # Fill the screen with night color if time is up pygame.display.update() time.sleep(3) # Wait for 3 seconds before restarting game_loop() # Restart the game from the beginning pygame.display.update() # Update the display # Start the game game_loop()
Editor is loading...
Leave a Comment