2нед



Информационные технологии
+3Как добавить сюда мнстров, или дайте промп для нейросети
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
import pygame
import math
import sys
# Настройки экрана
pygame.init()
info = pygame.display.Info()
WIDTH, HEIGHT = info.current_w, info.current_h
HALF_HEIGHT = HEIGHT // 2
FOV = math.pi / 3
HALF_FOV = FOV / 2
NUM_RAYS = 300
MAX_DEPTH = 20
DELTA_ANGLE = FOV / NUM_RAYS
DIST = NUM_RAYS / (2 * math.tan(HALF_FOV))
PROJ_COEFF = 3 * DIST * 40
SCALE = WIDTH // NUM_RAYS
# Загрузка текстуры стены
wall_texture = pygame.image.load("wall.png") # Укажи путь к изображению
wall_texture = pygame.transform.scale(wall_texture, (50, 50)) # Масштабирование
# Загрузка изображения дробовика
shotgun_image = pygame.image.load("ShootGun.png") # Укажи путь к изображению
shotgun_image = pygame.transform.scale(shotgun_image, (WIDTH // 3, HEIGHT // 3))
# Карта
MAP = [
[1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,1,0,0,1,0,1,0,0,1],
[1,0,0,1,0,0,1,0,1,0,0,1],
[1,0,0,0,0,1,0,0,0,0,0,1],
[1,0,1,1,0,0,0,1,1,0,0,1],
[1,0,0,0,0,1,0,0,0,0,0,1],
[1,0,1,0,0,0,0,1,0,1,0,1],
[1,0,0,0,1,0,1,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1,1,1],
]
MAP_WIDTH = len(MAP[0])
MAP_HEIGHT = len(MAP)
TILE = 50
# Игрок
player_pos = [TILE * 2.5, TILE * 2.5]
player_angle = 0
player_speed = 1.5
shotgun_offset = 0 # Анимация качания дробовика
def mapping(x, y):
return int(x // TILE), int(y // TILE)
sc = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
pygame.mouse.set_visible(False)
pygame.event.set_grab(True)
clock = pygame.time.Clock()
def draw_background():
pygame.draw.rect(sc, (100, 100, 100), (0, 0, WIDTH, HALF_HEIGHT))
pygame.draw.rect(sc, (40, 40, 40), (0, HALF_HEIGHT, WIDTH, HALF_HEIGHT))
def ray_casting(player_pos, player_angle):
cur_angle = player_angle - HALF_FOV
xo, yo = player_pos
walls = []
for ray in range(NUM_RAYS):
sin_a = math.sin(cur_angle)
cos_a = math.cos(cur_angle)
for depth in range(1, MAX_DEPTH * TILE, 2):
x = xo + depth * cos_a
y = yo + depth * sin_a
i, j = mapping(x, y)
if 0 <= i < MAP_WIDTH and 0 <= j < MAP_HEIGHT:
if MAP[j][i]:
depth *= math.cos(player_angle - cur_angle)
proj_height = min(int(PROJ_COEFF / (depth + 0.0001)), 2 * HEIGHT)
darkness = max(0.2, 1 - depth / (MAX_DEPTH * TILE / 2))
walls.append((ray * SCALE, HALF_HEIGHT - proj_height // 2, SCALE, proj_height, darkness))
break
cur_angle += DELTA_ANGLE
return walls
def draw_walls(walls):
for x, y, w, h, darkness in walls:
texture = pygame.transform.scale(wall_texture, (w, h))
dark_texture = texture.copy()
dark_texture.fill((darkness * 255, darkness * 255, darkness * 255), special_flags=pygame.BLEND_MULT)
sc.blit(dark_texture, (x, y))
def draw_shotgun():
shotgun_x = WIDTH // 2 - shotgun_image.get_width() // 2
shotgun_y = HEIGHT - shotgun_image.get_height() + shotgun_offset
sc.blit(shotgun_image, (shotgun_x, shotgun_y))
def game_loop():
global player_angle, shotgun_offset
mouse_sens = 0.002
shooting = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # Левая кнопка мыши
shooting = True
shotgun_offset = -20 # Анимация качания дробовика
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
shooting = False
shotgun_offset = 0 # Вернуть обратно
keys = pygame.key.get_pressed()
dx = dy = 0
if keys[pygame.K_w]:
dx += math.cos(player_angle) * player_speed
dy += math.sin(player_angle) * player_speed
if keys[pygame.K_s]:
dx -= math.cos(player_angle) * player_speed
dy -= math.sin(player_angle) * player_speed
if keys[pygame.K_a]:
dx += math.sin(player_angle) * player_speed
dy -= math.cos(player_angle) * player_speed
if keys[pygame.K_d]:
dx -= math.sin(player_angle) * player_speed
dy += math.cos(player_angle) * player_speed
next_x = player_pos[0] + dx
next_y = player_pos[1] + dy
if MAP[int(next_y // TILE)][int(next_x // TILE)] == 0:
player_pos[0] = next_x
player_pos[1] = next_y
mx, my = pygame.mouse.get_rel()
player_angle += mx * mouse_sens
draw_background()
walls = ray_casting(player_pos, player_angle)
draw_walls(walls)
draw_shotgun()
pygame.display.flip()
clock.tick(60)
game_loop()
По дате
По рейтингу
зачем же на пайгейме так извращаться, когда есть движки