リンクを新しいタブで開く
  1. Snake Game Without Pygame - Python Tutorial - CodePal

  1. 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 random
    import os
    import time

    class SnakeGame:
    def __init__(self, width=20, height=10):
    self.width = width
    self.height = height
    self.snake = [(width // 2, height // 2)]
    self.food = self.generate_food()
    self.score = 0
    self.direction = "RIGHT"
    self.game_over = False

    def 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 food

    def 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 += 1
    self.food = self.generate_food()
    else:
    self.snake.pop()

    if (
    new_head[0] in [0, self.width - 1] or
    new_head[1] in [0, self.height - 1] or
    new_head in self.snake[1:]
    ):
    self.game_over = True

    def 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 = direction

    def play_game(self):
    while not self.game_over:
    self.display_board()
    self.move_snake()
    time.sleep(0.2)
    if os.name == "nt":
    import msvcrt
    if 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, termios
    old_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()
    コピーしました。
    フィードバック
    ありがとうございました!詳細をお聞かせください
  2. 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 …

  3. 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

  4. 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 …

  5. Snake Game Help- not using Pygame(Python) - Stack Overflow

    Currently working on designing a snake game using python, with import.draw, without pygame!

  6. 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 …

  7. 他の人も質問しています
    読み込んでいます
    回答を読み込めません
  8. 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 …

  9. 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.

  10. 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.

  11. 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.

  12. 他の人は以下も検索しています

    Python Snake Game Code without Pygame について掘り下げる