init pygame
This commit is contained in:
commit
36abb693f8
1 changed files with 79 additions and 0 deletions
79
main.py
Normal file
79
main.py
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# Initialize Pygame
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
# Screen dimensions
|
||||||
|
WIDTH, HEIGHT = 800, 600
|
||||||
|
screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
||||||
|
pygame.display.set_caption('Pygame FPS and Event Display')
|
||||||
|
|
||||||
|
# Colors
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
WHITE = (255, 255, 255)
|
||||||
|
RED = (255, 0, 0)
|
||||||
|
|
||||||
|
# Set up the clock for managing the frame rate
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
font = pygame.font.Font(None, 36)
|
||||||
|
|
||||||
|
# Object properties
|
||||||
|
object_size = 50
|
||||||
|
object_x = WIDTH // 2
|
||||||
|
object_y = HEIGHT // 2
|
||||||
|
object_speed = 5
|
||||||
|
|
||||||
|
# Event text
|
||||||
|
event_text = ""
|
||||||
|
|
||||||
|
# Main loop
|
||||||
|
running = True
|
||||||
|
while running:
|
||||||
|
# Handle events
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
running = False
|
||||||
|
if event.type in [pygame.KEYDOWN, pygame.KEYUP]:
|
||||||
|
event_text = f"Event: {pygame.event.event_name(event.type)}, Key: {pygame.key.name(event.key)}"
|
||||||
|
print(event_text)
|
||||||
|
|
||||||
|
# Get the current state of all keyboard buttons
|
||||||
|
keys = pygame.key.get_pressed()
|
||||||
|
if keys[pygame.K_LEFT]:
|
||||||
|
object_x -= object_speed
|
||||||
|
if keys[pygame.K_RIGHT]:
|
||||||
|
object_x += object_speed
|
||||||
|
if keys[pygame.K_UP]:
|
||||||
|
object_y -= object_speed
|
||||||
|
if keys[pygame.K_DOWN]:
|
||||||
|
object_y += object_speed
|
||||||
|
|
||||||
|
# Ensure the object stays within the screen bounds
|
||||||
|
object_x = max(0, min(WIDTH - object_size, object_x))
|
||||||
|
object_y = max(0, min(HEIGHT - object_size, object_y))
|
||||||
|
|
||||||
|
# Clear the screen
|
||||||
|
screen.fill(BLACK)
|
||||||
|
|
||||||
|
# Draw the moveable object
|
||||||
|
pygame.draw.rect(screen, RED, (object_x, object_y, object_size, object_size))
|
||||||
|
|
||||||
|
# Calculate and display FPS
|
||||||
|
fps = clock.get_fps()
|
||||||
|
fps_text = font.render(f'FPS: {int(fps)}', True, WHITE)
|
||||||
|
screen.blit(fps_text, (10, 10))
|
||||||
|
|
||||||
|
# Display the event text
|
||||||
|
event_surface = font.render(event_text, True, WHITE)
|
||||||
|
screen.blit(event_surface, (10, 50))
|
||||||
|
|
||||||
|
# Update the display
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
# Cap the frame rate at 60 FPS
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
# Clean up and close the Pygame window
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
Loading…
Reference in a new issue