a new action game

star
mail@pastecode.io avatar
unknown
plain_text
a year ago
1.9 kB
1
Indexable
Never
import pygame

pygame.init()

# set up the game window
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
game_display = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Action Game')

# set up the game clock
clock = pygame.time.Clock()

# set up the player
player_width = 50
player_height = 50
player_x = WINDOW_WIDTH / 2 - player_width / 2
player_y = WINDOW_HEIGHT - player_height - 10
player_speed = 5
player_image = pygame.image.load('player.png')

# set up the enemies
enemy_width = 50
enemy_height = 50
enemy_x = WINDOW_WIDTH / 2 - enemy_width / 2
enemy_y = 10
enemy_speed = 3
enemy_image = pygame.image.load('enemy.png')

# game loop
while True:
    # event handling
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
    
    # move the player
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and player_x > 0:
        player_x -= player_speed
    if keys[pygame.K_RIGHT] and player_x < WINDOW_WIDTH - player_width:
        player_x += player_speed
    
    # move the enemy
    enemy_y += enemy_speed
    if enemy_y > WINDOW_HEIGHT:
        enemy_y = 10
        enemy_x = pygame.randint(0, WINDOW_WIDTH - enemy_width)
    
    # check for collisions
    player_rect = pygame.Rect(player_x, player_y, player_width, player_height)
    enemy_rect = pygame.Rect(enemy_x, enemy_y, enemy_width, enemy_height)
    if player_rect.colliderect(enemy_rect):
        print('Game over')
        pygame.quit()
        quit()
    
    # draw the game objects
    game_display.fill((255, 255, 255))
    game_display.blit(player_image, (player_x, player_y))
    game_display.blit(enemy_image, (enemy_x, enemy_y))
    
    # update the game window
    pygame.display.update()
    
    # set the game clock
    clock.tick(60)