نموذج لعبة snake

  1. الملف الرئيسي

from turtle import Turtle, Screen
from snake import Snake
from food import Food
from scoreboard import Score_board
import time

Set up the screen

window = Screen()
window.setup(600, 600, 0, 0)
window.bgcolor(‘black’)
window.title(“Snake Game”)

window.tracer(False)

instruction = Turtle()
snake = Snake()
food = Food()
score_game = Score_board()

Key bindings

window.listen()
window.onkey(snake.up, ‘Up’)
window.onkey(snake.down, ‘Down’)
window.onkey(snake.right, ‘Right’)
window.onkey(snake.left, ‘Left’)
window.onkey(snake.start_moving, ‘space’)

game_is_on = True
while game_is_on:
window.update()
time.sleep(0.1)
#window.bgcolor(‘cyan’)
instruction.color(‘white’)
instruction.hideturtle()
instruction.penup()
instruction.goto(-250, 100)
instruction.write(“The Game Instructions:\n\n1. To start moving: Press Space\n2. To move up: Press Up\n3. To move down: Press Down\n4. To move right: Press Right\n5. To move left: Press Left\n6. You will lose if the snake’s head touches the wall\n7. You will lose if the snake’s head touches its own body”, align=‘left’, font=(‘Calibri’, 11, ‘normal’))

if snake.is_moving:
    instruction.clear()
    snake.move()

if snake.head.distance(food) < 15:
    food.appear()
    snake.extend()
    score_game.update_score()

for segment in snake.snakes[:-1]:
    if snake.head.distance(segment) < 10:
        game_is_on = False
        window.bgcolor("dark red")
        score_game.game_over()

if snake.head.xcor() > 265 or snake.head.xcor() < -265 or snake.head.ycor() > 265 or snake.head.ycor() < -265:
    game_is_on = False
    window.bgcolor("dark red")
    score_game.game_over()

window.exitonclick()

  1. ملف الثعبان

from turtle import Turtle
import random
import time

angles = (90, 0, 0, 0)

class Snake:
def init(self):
self.position = [(-40, 0), (-20, 0), (0, 0)]
self.snakes =

    # Create the snake
    self.create_snake()
    self.head = self.snakes[-1]
    self.is_moving = False

def create_snake(self):
    for position in self.position:
        new_snake = Turtle()
        new_snake.penup()
        new_snake.shape('square')
        new_snake.color('white')
        new_snake.goto(position)

        # Use body gif shape for each segment
        new_snake.shape("square")
        self.snakes.append(new_snake)

def move(self):
    for i in range(len(self.snakes) - 1):
        self.snakes[i].goto(self.snakes[i + 1].pos())
    self.head.forward(20)

def start_moving(self):
    self.is_moving = True

def up(self):
    if self.head.heading() != 270:
        self.head.setheading(90)
    

def down(self):
    if self.head.heading() != 90:
        self.head.setheading(270)
       

def right(self):
    if self.head.heading() != 180:
        self.head.setheading(0)
      

def left(self):
    if self.head.heading() != 0:
        self.head.setheading(180)
        

def extend(self):
    new_segment = Turtle()
    new_segment.shape('square')
    new_segment.color('white')
    new_segment.penup()
    new_segment.goto(self.snakes[0].pos())
    self.snakes.insert(0, new_segment)
  1. ملف الطعام

from turtle import Turtle
import random

class Food(Turtle):
def init(self):
super().init()
# Set up the food object with the circle shape
self.shape(‘circle’)
self.color(‘red’)
self.shapesize(0.5,0.5)
self.penup()
self.appear()

def appear(self):
    # Randomly position the food on the screen
    food_x = random.randint(-230, 230)
    food_y = random.randint(-230, 230)
    self.goto(food_x, food_y)
  1. ملف لوحة النتبجة

from turtle import Turtle

class Score_board(Turtle):
def init(self):
super().init()
self.score = 0
self.shape(‘classic’)
self.color(‘white’)
self.penup()
self.goto(0,268)

    self.hideturtle()
    self.score_board()

def score_board(self):
    self.write(f"Score: {self.score}", align='center', font=('Arial', 20, 'bold'))

def update_score(self):
    self.score = self.score + 1
    self.clear()
    self.score_board()

def game_over(self):
    self.goto(0,0)
    
    self.write(f"Game Over! \nFinal Score: {self.score}", align='center', font=('Arial', 30, 'bold'))
  1. صور للعبة


5 إعجابات

جميل اضافة شرح للعبة تجعل اللعبة مفهومة اكثر :ok_hand:

إعجابَين (2)

شكرا لك على المشاركة القيمة :+1:

إعجابَين (2)

ماشاء الله يعطيك العافيه

إعجابَين (2)

شكرا لتفاعلك. بصراحة انا لست جيدا في تنسيق المواضيع في المنتديات بصفة عامة حتى لا اعلم كيفية رفع ملف اللعبة كامل للمشاركة.

إعجابَين (2)

جميلة الفكرة انك تكتب القواعد طريقة اللعب وكذا صارت اللعبة افضل
شكرا على المشاركة👍

إعجاب واحد (1)