Загрузить файлы в «/»
Lai kods stradātu vēl japievieno modeļus "player.png", "enemy.png", "bullet.png"assets
parent
6b2a8fa21f
commit
e7026a39bf
|
@ -0,0 +1,78 @@
|
|||
import pygame
|
||||
import math
|
||||
import sys
|
||||
|
||||
# Инициализация Pygame
|
||||
pygame.init()
|
||||
|
||||
# Настройки экрана
|
||||
WIDTH, HEIGHT = 800, 600
|
||||
screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
||||
pygame.display.set_caption("Shooting Game")
|
||||
|
||||
# Загрузка изображения пули и персонажей
|
||||
bullet_img = pygame.transform.scale(pygame.image.load('bullet.png'), (50, 100))
|
||||
player_img = pygame.transform.scale(pygame.image.load('player.png'), (50, 30))
|
||||
enemy_img = pygame.transform.scale(pygame.image.load('enemy.png'), (50, 30))
|
||||
crosshair_img = pygame.transform.scale(pygame.image.load('crosshair.png'), (30, 30))
|
||||
|
||||
# Позиции игрока и врага
|
||||
player_pos = [WIDTH // 2 - player_img.get_width() // 2, HEIGHT - player_img.get_height()]
|
||||
enemy_pos = [WIDTH // 2 - enemy_img.get_width() // 2, 50]
|
||||
|
||||
# Скорость перемещения игрока
|
||||
player_speed = 5
|
||||
|
||||
# Списки для хранения пуль игрока и врага
|
||||
player_bullets = []
|
||||
|
||||
# Функция для отрисовки объектов
|
||||
def draw_elements():
|
||||
screen.fill((255, 255, 255)) # Фон белого цвета
|
||||
screen.blit(player_img, player_pos)
|
||||
screen.blit(enemy_img, enemy_pos)
|
||||
screen.blit(crosshair_img, pygame.mouse.get_pos()) # Отображение мушки
|
||||
for bullet in player_bullets:
|
||||
screen.blit(bullet_img, bullet["position"])
|
||||
|
||||
# Основной игровой цикл
|
||||
while True:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: # Стрельба по нажатию ЛКМ
|
||||
mouse_x, mouse_y = pygame.mouse.get_pos() # Получаем координаты курсора мыши
|
||||
angle = math.atan2(mouse_y - player_pos[1], mouse_x - player_pos[0]) # Вычисляем угол между игроком и курсором
|
||||
player_bullets.append({"position": [player_pos[0] + player_img.get_width() // 2 - bullet_img.get_width() // 2,
|
||||
player_pos[1] + player_img.get_height() // 2 - bullet_img.get_height() // 2],
|
||||
"angle": angle})
|
||||
|
||||
keys = pygame.key.get_pressed()
|
||||
if keys[pygame.K_w] and player_pos[1] > 0: # Перемещение вверх
|
||||
player_pos[1] -= player_speed
|
||||
if keys[pygame.K_s] and player_pos[1] < HEIGHT - player_img.get_height(): # Перемещение вниз
|
||||
player_pos[1] += player_speed
|
||||
if keys[pygame.K_a] and player_pos[0] > 0: # Перемещение влево
|
||||
player_pos[0] -= player_speed
|
||||
if keys[pygame.K_d] and player_pos[0] < WIDTH - player_img.get_width(): # Перемещение вправо
|
||||
player_pos[0] += player_speed
|
||||
|
||||
# Обновление позиции пуль игрока
|
||||
for bullet in player_bullets:
|
||||
bullet["position"][0] += 5 * math.cos(bullet["angle"]) # Учитываем направление пули
|
||||
bullet["position"][1] += 5 * math.sin(bullet["angle"]) # Учитываем направление пули
|
||||
|
||||
# Удаляем пули, которые выходят за пределы экрана
|
||||
if bullet["position"][0] < 0 or bullet["position"][0] > WIDTH or bullet["position"][1] < 0 or bullet["position"][1] > HEIGHT:
|
||||
player_bullets.remove(bullet)
|
||||
|
||||
# Отрисовка элементов
|
||||
draw_elements()
|
||||
|
||||
clock = pygame.time.Clock()
|
||||
dt = 0
|
||||
|
||||
dt = clock.tick(60) / 1000
|
||||
# Обновление экрана
|
||||
pygame.display.flip()
|
Loading…
Reference in New Issue