Untitled
unknown
plain_text
a year ago
7.3 kB
11
Indexable
import pygame
import random
import time
pygame.init()
screen_height = 800
screen_width = 1200
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Game Menu Example')
player = pygame.Rect(300, 250, 50, 50)
enemy = pygame.Rect(600, 400, 50, 50)
bullets = []
running = True
direction = 'U'
bullet_created = time.time()
ai_bullet_created = time.time()
enemy_lives = 3
player_lives = 3
difficulties = ['Easy', 'Medium', 'Hard']
selected_difficulty_index = 0
difficulty = difficulties[selected_difficulty_index]
def create_bullet(direction, box):
global bullets
bullet_direction = direction
switch = False
if direction == 'U':
x, y = box.midtop
x -= 2.5
y -= 7.5
switch = True
bullet_loc = {'x': x, 'y': y}
elif direction == 'D':
x, y = box.midbottom
x -= 2.5
switch = True
bullet_loc = {'x': x, 'y': y}
elif direction == 'R':
x, y = box.midright
bullet_loc = {'x': x, 'y': y}
elif direction == 'L':
x, y = box.midleft
x -= 7.5
y -= 2.5
bullet_loc = {'x': x, 'y': y}
if switch:
bullet_name = pygame.Rect(x, y, 5, 10)
else:
bullet_name = pygame.Rect(x, y, 10, 5)
bullet = {
'name': bullet_name,
'direction': bullet_direction,
'location': bullet_loc
}
bullets.append(bullet)
def update_bullets(bullets):
global running, player_lives, enemy_lives
for bullet in bullets:
bullet_name = bullet['name']
direction = bullet['direction']
bullet_loc = bullet['location']
if direction == 'U':
bullet_name.move_ip(0, -3)
elif direction == 'D':
bullet_name.move_ip(0, 3)
elif direction == 'R':
bullet_name.move_ip(3, 0)
elif direction == 'L':
bullet_name.move_ip(-3, 0)
if not (bullet_loc['x'] < 0 or bullet_loc['y'] < 0 or bullet_loc['x'] > screen_width or bullet_loc['y'] > screen_height):
pygame.draw.rect(screen, (255, 255, 255), bullet_name)
else:
bullets.remove(bullet)
if bullet_name.colliderect(player):
player_lives -= 1
bullets.remove(bullet)
if bullet_name.colliderect(enemy):
enemy_lives -= 1
bullets.remove(bullet)
def enemy_update(player, enemy, current_time):
global ai_bullet_created
x, y = player.center
if difficulty == 'Easy':
x = random.randrange(-500, 500) + x
y = random.randrange(-500, 500) + y
elif difficulty == 'Medium':
x = random.randrange(-300, 300) + x
y = random.randrange(-300, 300) + y
elif difficulty == 'Hard':
x = random.randrange(-100, 100) + x
y = random.randrange(-100, 100) + y
myx, myy = enemy.center
diff_x = abs(x - myx)
diff_y = abs(y - myy)
if diff_x > diff_y:
actudiff_y = y - myy
if actudiff_y < 0:
enemy.move_ip(0, -1)
elif actudiff_y > 0:
enemy.move_ip(0, 1)
elif (current_time - ai_bullet_created) > 0.5:
ai_bullet_created = time.time()
actudiff_x = x - myx
if actudiff_x < 0:
create_bullet('L', enemy)
elif actudiff_x > 0:
create_bullet('R', enemy)
if diff_x < diff_y:
actudiff_x = x - myx
if actudiff_x < 0:
enemy.move_ip(-1, 0)
elif actudiff_x > 0:
enemy.move_ip(1, 0)
elif (current_time - ai_bullet_created) > 0.5:
ai_bullet_created = time.time()
actudiff_y = y - myy
if actudiff_y < 0:
create_bullet('U', enemy)
elif actudiff_y > 0:
create_bullet('D', enemy)
def check_outside(box):
global screen_width, screen_height
x, y = box.center
if x < 0:
box.move_ip(5, 0)
if x > screen_width:
box.move_ip(-5, 0)
if y < 0:
box.move_ip(0, 5)
if y > screen_height:
box.move_ip(0, -5)
def draw_menu():
screen.fill((0, 0, 0))
font = pygame.font.Font(None, 74)
title_text = font.render('Game Menu', True, (255, 255, 255))
screen.blit(title_text, (screen_width // 2 - title_text.get_width() // 2, screen_height // 4))
font = pygame.font.Font(None, 36)
for i, diff in enumerate(difficulties):
color = (255, 255, 0) if i == selected_difficulty_index else (255, 255, 255)
diff_text = font.render(diff, True, color)
screen.blit(diff_text, (screen_width // 2 - diff_text.get_width() // 2, screen_height // 2 + i * 40 - 40))
start_text = font.render('Press Enter to Start', True, (255, 255, 255))
screen.blit(start_text, (screen_width // 2 - start_text.get_width() // 2, screen_height // 2 + 100))
pygame.display.flip()
menu_running = True
while menu_running:
draw_menu()
for event in pygame.event.get():
if event.type == pygame.QUIT:
menu_running = False
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
menu_running = False
difficulty = difficulties[selected_difficulty_index]
elif event.key == pygame.K_UP:
selected_difficulty_index = (selected_difficulty_index - 1) % len(difficulties)
elif event.key == pygame.K_DOWN:
selected_difficulty_index = (selected_difficulty_index + 1) % len(difficulties)
while running:
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (0, 0, 255), player)
pygame.draw.rect(screen, (255, 0, 0), enemy)
key = pygame.key.get_pressed()
if key[pygame.K_a]:
player.move_ip(-2, 0)
direction = 'L'
elif key[pygame.K_d]:
player.move_ip(2, 0)
direction = 'R'
elif key[pygame.K_w]:
player.move_ip(0, -2)
direction = 'U'
elif key[pygame.K_s]:
player.move_ip(0, 2)
direction = 'D'
current_time = time.time()
if key[pygame.K_SPACE] and (current_time - bullet_created) > 0.5:
create_bullet(direction, player)
bullet_created = time.time()
update_bullets(bullets)
if enemy_lives > 0:
enemy_update(player, enemy, current_time)
else:
running = False
print('You Won!')
if player_lives <= 0:
running = False
print('You Lost.')
check_outside(player)
check_outside(enemy)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
direction = 'U'
elif event.key == pygame.K_DOWN:
direction = 'D'
elif event.key == pygame.K_LEFT:
direction = 'L'
elif event.key == pygame.K_RIGHT:
direction = 'R'
pygame.display.flip()
pygame.quit()
Editor is loading...
Leave a Comment