Add mygame.py
This commit is contained in:
parent
f4c7f472ec
commit
668421da9d
1 changed files with 123 additions and 0 deletions
123
mygame.py
Normal file
123
mygame.py
Normal file
|
@ -0,0 +1,123 @@
|
|||
import pygame
|
||||
import sdl2
|
||||
import sdl2.ext
|
||||
import sys
|
||||
|
||||
# Initialize Pygame
|
||||
pygame.init()
|
||||
|
||||
# Initialize SDL2
|
||||
if sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) != 0:
|
||||
print(f"SDL2 initialization failed: {sdl2.SDL_GetError().decode('utf-8')}")
|
||||
exit(1)
|
||||
|
||||
# Screen dimensions
|
||||
WIDTH, HEIGHT = 640, 480
|
||||
sdl_window = sdl2.SDL_CreateWindow(b"Pygame with SDL2 Rendering",
|
||||
sdl2.SDL_WINDOWPOS_CENTERED,
|
||||
sdl2.SDL_WINDOWPOS_CENTERED,
|
||||
WIDTH, HEIGHT,
|
||||
sdl2.SDL_WINDOW_SHOWN)
|
||||
|
||||
if not sdl_window:
|
||||
print(f"Window creation failed: {sdl2.SDL_GetError().decode('utf-8')}")
|
||||
sdl2.SDL_Quit()
|
||||
exit(1)
|
||||
|
||||
sdl_renderer = sdl2.SDL_CreateRenderer(sdl_window, -1, sdl2.SDL_RENDERER_ACCELERATED)
|
||||
if not sdl_renderer:
|
||||
print(f"Renderer creation failed: {sdl2.SDL_GetError().decode('utf-8')}")
|
||||
sdl2.SDL_DestroyWindow(sdl_window)
|
||||
sdl2.SDL_Quit()
|
||||
exit(1)
|
||||
|
||||
# Colors
|
||||
BLACK = sdl2.ext.Color(0, 0, 0)
|
||||
WHITE = sdl2.ext.Color(255, 255, 255)
|
||||
RED = sdl2.ext.Color(255, 0, 0)
|
||||
|
||||
# Set up the clock for managing the frame rate
|
||||
clock = pygame.time.Clock()
|
||||
font = pygame.font.Font(None, 24) # Reduce font size for better fit
|
||||
|
||||
# Object properties
|
||||
object_size = 50
|
||||
object_x = WIDTH // 2
|
||||
object_y = HEIGHT // 2
|
||||
object_speed = 5
|
||||
|
||||
# Event text
|
||||
event_text = ""
|
||||
|
||||
# Helper function to create SDL texture from Pygame surface
|
||||
def create_texture_from_surface(surface):
|
||||
width, height = surface.get_size()
|
||||
texture = sdl2.SDL_CreateTexture(sdl_renderer,
|
||||
sdl2.SDL_PIXELFORMAT_ARGB8888,
|
||||
sdl2.SDL_TEXTUREACCESS_STREAMING,
|
||||
width, height)
|
||||
pixels = pygame.image.tostring(surface, "ARGB")
|
||||
sdl2.SDL_UpdateTexture(texture, None, pixels, width * 4)
|
||||
return texture
|
||||
|
||||
# Main loop
|
||||
running = True
|
||||
while running:
|
||||
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
|
||||
sdl2.SDL_SetRenderDrawColor(sdl_renderer, WHITE.r, WHITE.g, WHITE.b, WHITE.a)
|
||||
sdl2.SDL_RenderClear(sdl_renderer)
|
||||
|
||||
# Draw the moveable object
|
||||
rect = sdl2.SDL_Rect(object_x, object_y, object_size, object_size)
|
||||
sdl2.SDL_SetRenderDrawColor(sdl_renderer, RED.r, RED.g, RED.b, RED.a)
|
||||
sdl2.SDL_RenderFillRect(sdl_renderer, rect)
|
||||
|
||||
# Calculate and display FPS
|
||||
fps = clock.get_fps()
|
||||
fps_surface = font.render(f'FPS: {int(fps)}', True, (0, 0, 0))
|
||||
fps_texture = create_texture_from_surface(fps_surface)
|
||||
sdl2.SDL_RenderCopy(sdl_renderer, fps_texture, None, sdl2.SDL_Rect(10, 10, fps_surface.get_width(), fps_surface.get_height()))
|
||||
|
||||
# Display the event text
|
||||
event_surface = font.render(event_text, True, (0, 0, 0))
|
||||
event_texture = create_texture_from_surface(event_surface)
|
||||
sdl2.SDL_RenderCopy(sdl_renderer, event_texture, None, sdl2.SDL_Rect(10, 50, event_surface.get_width(), event_surface.get_height()))
|
||||
|
||||
# Update the display
|
||||
sdl2.SDL_RenderPresent(sdl_renderer)
|
||||
|
||||
# Cap the frame rate at 60 FPS
|
||||
clock.tick(60)
|
||||
|
||||
# Destroy textures to avoid memory leaks
|
||||
sdl2.SDL_DestroyTexture(fps_texture)
|
||||
sdl2.SDL_DestroyTexture(event_texture)
|
||||
|
||||
# Clean up and close the SDL2 window
|
||||
sdl2.SDL_DestroyRenderer(sdl_renderer)
|
||||
sdl2.SDL_DestroyWindow(sdl_window)
|
||||
sdl2.SDL_Quit()
|
||||
pygame.quit()
|
||||
sys.exit()
|
Loading…
Reference in a new issue