window_flags = pygame.NOFRAME
screen = pygame.display.set_mode((# здесь указываешь ширину экрана, # а здесь высоту экрана), window_flags)
info = pygame.display.Info()
# получаем размеры экрана
screen_width = info.current_w
screen_height = info.current_h
что делать если при открытии через python появляется что то на подобии командной строки, а после выходит из нее?Посмотреть вложение 201847
Тыкаешь сюда( там где у тебя py-файл )
и вводишь команду python *название*.py
Это если у тебя python скачан на пк
значит у тебя нет цикла. Он выполняет программу и закрываетсячто делать если при открытии через python появляется что то на подобии командной строки, а после выходит из нее?
а как его сделать и куда вставить?значит у тебя нет цикла. Он выполняет программу и закрывается
скинь код свой
import random
import pygame as pg
WSIZE = (720, 480)
screen = pg.display.set_mode(WSIZE)
TSIDE = 30
MSIZE = WSIZE[0] // TSIDE, WSIZE[1] // TSIDE
start_pos = MSIZE[0] // 2, MSIZE[1] // 2
snake = [start_pos]
alive = True
direction = 0
directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]
apple = random.randint(0, MSIZE[0]-1), random.randint(0, MSIZE[1]-1)
fps = 5
clock = pg.time.Clock()
pg.font.init()
font_score = pg.font.SysFont("Arial", 25)
font_gameover = pg.font.SysFont("Arial", 45)
font_space = pg.font.SysFont("Arial", 18)
running = True
while running:
clock.tick(fps)
screen.fill("black")
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
if event.type == pg.KEYDOWN:
if alive:
if event.key == pg.K_RIGHT and direction != 2:
direction = 0
if event.key == pg.K_DOWN and direction != 3:
direction = 1
if event.key == pg.K_LEFT and direction != 0:
direction = 2
if event.key == pg.K_UP and direction != 1:
direction = 3
else:
if event.key == pg.K_SPACE:
alive = True
snake = [start_pos]
apple = random.randint(0, MSIZE[0]-1), random.randint(0, MSIZE[1]-1)
fps = 5
[pg.draw.rect(screen, "green", (x * TSIDE, y * TSIDE, TSIDE - 1, TSIDE - 1)) for x, y in snake]
pg.draw.rect(screen, "red", (apple[0] * TSIDE, apple[1] * TSIDE, TSIDE - 1, TSIDE - 1))
if alive:
new_pos = snake[0][0] + directions[direction][0], snake[0][1] + directions[direction][1]
if not (0 <= new_pos[0] < MSIZE[0] and 0 <= new_pos[1] < MSIZE[1]) or \
new_pos in snake:
alive = False
else:
snake.insert(0, new_pos)
if new_pos == apple:
fps += 1
apple = random.randint(0, MSIZE[0]-1), random.randint(0, MSIZE[1]-1)
else:
snake.pop(-1)
else:
text = font_gameover.render(f"GAME OVER", True, "white")
screen.blit(text, (WSIZE[0] // 2 - text.get_width()//2, WSIZE[1] // 2 - 50))
text = font_space.render(f"Press SPACE for restart", True, "white")
screen.blit(text, (WSIZE[0] // 2 - text.get_width() // 2, WSIZE[1] // 2 + 10))
screen.blit(font_score.render(f"Score: {len(snake)}", True, "yellow"), (5, 5))
pg.display.flip()
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the game window
window_width, window_height = 640, 480
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("Snake Game")
# Define colors
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
# Set up the game clock
clock = pygame.time.Clock()
# Set up the font
font = pygame.font.Font(None, 36)
# Set up the snake and food initial positions
snake_position = [100, 50]
snake_body = [[100, 50], [90, 50], [80, 50]]
food_position = [random.randrange(1, (window_width // 10)) * 10,
random.randrange(1, (window_height // 10)) * 10]
food_spawned = True
# Set up the initial snake direction
direction = 'RIGHT'
change_to = direction
# Set up the game over flag
game_over = False
# Game loop
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT or event.key == ord('d'):
change_to = 'RIGHT'
elif event.key == pygame.K_LEFT or event.key == ord('a'):
change_to = 'LEFT'
elif event.key == pygame.K_UP or event.key == ord('w'):
change_to = 'UP'
elif event.key == pygame.K_DOWN or event.key == ord('s'):
change_to = 'DOWN'
# Update the snake direction
if change_to == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
elif change_to == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
elif change_to == 'UP' and direction != 'DOWN':
direction = 'UP'
elif change_to == 'DOWN' and direction != 'UP':
direction = 'DOWN'
# Update the snake position
if direction == 'RIGHT':
snake_position[0] += 10
elif direction == 'LEFT':
snake_position[0] -= 10
elif direction == 'UP':
snake_position[1] -= 10
elif direction == 'DOWN':
snake_position[1] += 10
# Increase snake's body length when it eats the food
snake_body.insert(0, list(snake_position))