Untitled
unknown
plain_text
2 months ago
1.7 kB
3
Indexable
import pygame import sys # Initialize Pygame pygame.init() # Set up the game window screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Attack Gun Game") # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) # Gun settings gun_width = 50 gun_height = 30 gun_x = screen_width // 2 gun_y = screen_height - gun_height - 10 gun_speed = 5 # Bullet settings bullet_width = 5 bullet_height = 10 bullet_speed = 7 bullets = [] # Set the frame rate clock = pygame.time.Clock() # Main game loop while True: screen.fill(WHITE) # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Get the keys pressed keys = pygame.key.get_pressed() # Move the gun with arrow keys if keys[pygame.K_LEFT] and gun_x > 0: gun_x -= gun_speed if keys[pygame.K_RIGHT] and gun_x < screen_width - gun_width: gun_x += gun_speed # Fire bullets when spacebar is pressed if keys[pygame.K_SPACE]: bullet_x = gun_x + gun_width // 2 - bullet_width // 2 bullet_y = gun_y bullets.append([bullet_x, bullet_y]) # Move bullets upwards for bullet in bullets[:]: bullet[1] -= bullet_speed if bullet[1] < 0: bullets.remove(bullet) # Draw the gun (a simple rectangle) pygame.draw.rect(screen, BLACK, (gun_x, gun_y, gun_width, gun_height)) # Draw the bullets for bullet in bullets: pygame.draw.rect(screen, RED, (bullet[0], bullet[1], bullet_width, bullet_height)) # Update the screen pygame.display.update() # Set the frame rate (FPS) clock.tick(60)
Editor is loading...
Leave a Comment