- ✕この概要は、複数のオンライン ソースに基づいて AI を使用して生成されました。元のソース情報を表示するには、[詳細情報] リンクを使用します。
Creating a snake game in Python without using the Pygame library involves using basic console input/output and handling game logic manually. Below is a simple implementation of the snake game.
import randomimport osimport timeclass SnakeGame:def __init__(self, width=20, height=10):self.width = widthself.height = heightself.snake = [(width // 2, height // 2)]self.food = self.generate_food()self.score = 0self.direction = "RIGHT"self.game_over = Falsedef generate_food(self):while True:food = (random.randint(1, self.width - 2), random.randint(1, self.height - 2))if food not in self.snake:return fooddef display_board(self):os.system("cls" if os.name == "nt" else "clear")for y in range(self.height):for x in range(self.width):if (x, y) == self.snake[0]:print("O", end="")elif (x, y) in self.snake:print("o", end="")elif (x, y) == self.food:print("X", end="")else:print(".", end="")print()print(f"Score: {self.score}")def move_snake(self):head_x, head_y = self.snake[0]if self.direction == "UP":new_head = (head_x, head_y - 1)elif self.direction == "DOWN":new_head = (head_x, head_y + 1)elif self.direction == "LEFT":new_head = (head_x - 1, head_y)elif self.direction == "RIGHT":new_head = (head_x + 1, head_y)self.snake.insert(0, new_head)if new_head == self.food:self.score += 1self.food = self.generate_food()else:self.snake.pop()if (new_head[0] in [0, self.width - 1] ornew_head[1] in [0, self.height - 1] ornew_head in self.snake[1:]):self.game_over = Truedef change_direction(self, direction):if ((direction == "UP" and self.direction != "DOWN") or(direction == "DOWN" and self.direction != "UP") or(direction == "LEFT" and self.direction != "RIGHT") or(direction == "RIGHT" and self.direction != "LEFT")):self.direction = directiondef play_game(self):while not self.game_over:self.display_board()self.move_snake()time.sleep(0.2)if os.name == "nt":import msvcrtif msvcrt.kbhit():key = msvcrt.getch().decode()if key == "w":self.change_direction("UP")elif key == "s":self.change_direction("DOWN")elif key == "a":self.change_direction("LEFT")elif key == "d":self.change_direction("RIGHT")else:import sys, tty, termiosold_settings = termios.tcgetattr(sys.stdin)try:tty.setcbreak(sys.stdin.fileno())key = sys.stdin.read(1)if key == "w":self.change_direction("UP")elif key == "s":self.change_direction("DOWN")elif key == "a":self.change_direction("LEFT")elif key == "d":self.change_direction("RIGHT")finally:termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)print("Game Over!")if __name__ == "__main__":game = SnakeGame()game.play_game()コピーしました。✕コピー Snake Game in Python (No Pygame) | snake_game
Learn how to create a classic Snake game in Python without using external libraries like Pygame. The game runs in the terminal and uses keyboard input, random food, score tracking, and collision …
snake in python, without any external libraries and only …
snake in python, without any external libraries and only built in python libraries - pure-python-snake-game.py
Create a Snake-Game using Turtle in Python
2026年2月18日 · The Snake Game is a classic arcade game first released in 1976 by Gremlin Industries and published by Sega. The goal is simple to control the …
Snake Game Help- not using Pygame(Python) - Stack Overflow
Currently working on designing a snake game using python, with import.draw, without pygame!
Build Snake Game in Python Using Turtle Module
2025年6月26日 · In this tutorial, I’ll walk you through creating a complete Snake game from scratch using Python’s Turtle module. The game includes all the …
- 他の人も質問しています
Console-based Snake game in Python - Code Review Stack Exchange
2020年4月23日 · I recently wrote this snake console game in Python, which avoids relying on libraries like pygame, tkinter or curses. It works just fine, but as I'm a rather junior programmer and this is my …
Make a snake game in PYTHON 3 without using pygame and using the ...
Make a snake game in PYTHON 3 without using pygame and using the provided code as the only graphics. def get_last_key (self):""" Returns the last key pressed.
Building a Snake Game in Python with Tkinter | PyShine
2026年1月28日 · A beginner-friendly tutorial explaining how to build a classic Snake game using Python and Tkinter.
Snake — Free Python Games 2.5.3 documentation - Grant Jenks
How do you make the snake faster or slower? 2. How can you make the snake go around the edges? 3. How would you move the food? 4.
Python Snake Game Code without Pygame について掘り下げる