Python3 Pygame PyOpenGL shooter game pls
- User
Python3 Pygame PyOpenGL shooter game pls
- Assistant
Python3 Pygame PyOpenGL shooter game pls
Sure! Below is a simple example of a 2D shooter game built using Python3 with Pygame and PyOpenGL. This example sets up a window where you can control a player square, shoot bullets, and have enemies spawn and move toward the player.
Before running the game, make sure you have the necessary libraries installed. You can install them using pip:
pip install pygame PyOpenGL
WASD to move around. Press the spacebar to shoot bullets.import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import random
import sys
# Initialize Pygame
pygame.init()
screen_width, screen_height = 800, 600
pygame.display.set_mode((screen_width, screen_height), DOUBLEBUF | OPENGL)
pygame.display.set_caption("Simple Shooter Game")
# Set up orthographic projection for 2D rendering
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0, screen_width, 0, screen_height)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
# Disable depth testing and lighting for 2D
glDisable(GL_DEPTH_TEST)
glDisable(GL_LIGHTING)
# Define colors
WHITE = (1, 1, 1)
RED = (1, 0, 0)
GREEN = (0, 1, 0)
BLUE = (0, 0, 1)
YELLOW = (1, 1, 0)
# Player properties
player_size = 50
player_speed = 5
player_pos = [screen_width // 2, player_size // 2 + 10]
# Bullet properties
bullet_width, bullet_height = 5, 10
bullet_speed = 7
bullets = []
# Enemy properties
enemy_size = 40
enemy_speed = 2
enemies = []
enemy_spawn_event = pygame.USEREVENT + 1
pygame.time.set_timer(enemy_spawn_event, 1000) # Spawn enemy every second
# Function to draw a rectangle
def draw_rect(color, position, size):
glColor3fv(color)
x, y = position
w, h = size
glBegin(GL_QUADS)
glVertex2f(x - w / 2, y - h / 2)
glVertex2f(x + w / 2, y - h / 2)
glVertex2f(x + w / 2, y + h / 2)
glVertex2f(x - w / 2, y + h / 2)
glEnd()
# Function to check collision between two rectangles
def check_collision(pos1, size1, pos2, size2):
x1, y1 = pos1
w1, h1 = size1
x2, y2 = pos2
w2, h2 = size2
return (
abs(x1 - x2) * 2 < (w1 + w2) and
abs(y1 - y2) * 2 < (h1 + h2)
)
# Game loop
clock = pygame.time.Clock()
running = True
while running:
clock.tick(60) # Limit to 60 FPS
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == enemy_spawn_event:
# Spawn an enemy at a random x position at the top
enemy_x = random.randint(enemy_size, screen_width - enemy_size)
enemy_y = screen_height + enemy_size
enemies.append([enemy_x, enemy_y])
# Handle keys
keys = pygame.key.get_pressed()
if keys[K_LEFT] or keys[K_a]:
player_pos[0] -= player_speed
if keys[K_RIGHT] or keys[K_d]:
player_pos[0] += player_speed
if keys[K_UP] or keys[K_w]:
player_pos[1] += player_speed
if keys[K_DOWN] or keys[K_s]:
player_pos[1] -= player_speed
if keys[K_SPACE]:
# Fire a bullet (limit the rate if needed)
if len(bullets) == 0 or pygame.time.get_ticks() - bullets[-1][2] > 300:
bullets.append([player_pos[0], player_pos[1] + player_size / 2 + bullet_height, pygame.time.get_ticks()])
# Update bullets
bullets = [[bx, by + bullet_speed, bt] for bx, by, bt in bullets if by < screen_height]
# Update enemies
enemies = [[ex, ey - enemy_speed] for ex, ey in enemies if ey > -enemy_size]
# Check collisions
for enemy in enemies[:]:
for bullet in bullets[:]:
if check_collision(enemy, (enemy_size, enemy_size), bullet, (bullet_width, bullet_height)):
enemies.remove(enemy)
bullets.remove(bullet)
break
# Clear screen
glClear(GL_COLOR_BUFFER_BIT)
# Draw player
draw_rect(BLUE, player_pos, (player_size, player_size))
# Draw bullets
for bullet in bullets:
draw_rect(YELLOW, (bullet[0], bullet[1]), (bullet_width, bullet_height))
# Draw enemies
for enemy in enemies:
draw_rect(RED, enemy, (enemy_size, enemy_size))
# Swap buffers
pygame.display.flip()
# Keep player within screen bounds
player_pos[0] = max(player_size / 2, min(screen_width - player_size / 2, player_pos[0]))
player_pos[1] = max(player_size / 2, min(screen_height - player_size / 2, player_pos[1]))
pygame.quit()
sys.exit()
Initialization:
Player, Bullets, and Enemies:
WASD.Drawing Function:
draw_rect is a helper function to draw rectangles (player, bullets, enemies) with specified colors, positions, and sizes.Collision Detection:
check_collision function checks if two rectangles overlap.Game Loop:
Install Dependencies: Ensure you have Python 3 installed. Then install Pygame and PyOpenGL using pip:
pip install pygame PyOpenGL
Save the Code:
Save the provided code in a file, for example, shooter_game.py.
Run the Game: Execute the game using Python:
python shooter_game.py
ADWSThis is a basic example to get you started. You can enhance the game by adding features such as:
Feel free to expand upon this foundation to create a more engaging and feature-rich shooter game!
Not yet included in a published release.