1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- # Simple pygame program
- # Import and initialize the pygame library
- import pygame
- import sys
- # Initialize Pygame
- pygame.init()
- # Set up the drawing window
- info = pygame.display.Info()
- screen_width = info.current_w
- screen_height = info.current_h
- GAME_WIDTH=240
- GAME_HEIGHT=160
- GAME_ASPECT = GAME_WIDTH / GAME_HEIGHT
- screen_aspect = screen_width / screen_height
- if screen_aspect > GAME_ASPECT:
- # Pillarbox (black bars on the sides)
- new_width = int(screen_height * GAME_ASPECT)
- new_height = screen_height
- x_offset = (screen_width - new_width) // 2
- y_offset = 0
- else:
- # Letterbox (black bars on top/bottom)
- new_width = screen_width
- new_height = int(screen_width / GAME_ASPECT)
- x_offset = 0
- y_offset = (screen_height - new_height) // 2
- screen = pygame.display.set_mode([screen_width, screen_height], pygame.FULLSCREEN)
- # Run until the user asks to quit
- running = True
- while running:
- # Did the user click the window close button?
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- # Get the state of all keys (whether pressed or not)
- keys = pygame.key.get_pressed()
- # Handle key states for continuous key checks
- if keys[pygame.K_UP]: # Handle UP arrow key
- print("Up key pressed")
- if keys[pygame.K_DOWN]: # Handle DOWN arrow key
- print("Down key pressed")
- if keys[pygame.K_LEFT]: # Handle LEFT arrow key
- print("Left key pressed")
- if keys[pygame.K_RIGHT]: # Handle RIGHT arrow key
- print("Right key pressed")
- if keys[pygame.K_z]: # Handle 'Z' key
- running = False
- print("Z key pressed")
- if keys[pygame.K_x]: # Handle 'X' key
- print("X key pressed")
- if keys[pygame.K_c]: # Handle 'C' key
- print("C key pressed")
- if keys[pygame.K_v]: # Handle 'V' key
- print("V key pressed")
- if keys[pygame.K_b]: # Handle 'B' key
- print("B key pressed")
- if keys[pygame.K_n]: # Handle 'N' key
- print("N key pressed")
- # Fill the background with white
- game_surface = pygame.Surface((GAME_WIDTH, GAME_HEIGHT))
- game_surface.fill((255, 255, 255))
- # Draw a solid blue circle in the center
- pygame.draw.circle(game_surface, (0, 0, 255), (120, 80), 35)
- scaled_game_surface = pygame.transform.scale(game_surface, (new_width, new_height))
- # Blit the scaled game surface to the center with black bars around it
- screen.blit(scaled_game_surface, (x_offset, y_offset))
- # Flip the display
- pygame.display.flip()
- # Done! Time to quit.
- pygame.quit()
|