أنشأت هذا التعديل بمساعدة تشات جي بي تي
الكود الذي أعطانيه تشات جي بي تي
-
يمكنك استخدام هذا الكود لتحاول بنفسك اعتبره تحدي
-
ملحوظة مهمة في تطبيق pydroid3
- بعد حفظ الملفين في مجلد اذا أردت التعديل على واحد منهما بعد التعديل لكي تحفظ اضغط فقط على save و ليس save as
import turtle
# Set up the screen
wn = turtle.Screen()
wn.title("Snake Game with Custom Tap Controls")
wn.bgcolor("black")
wn.setup(width=600, height=600)
# Create the snake head
head = turtle.Turtle()
head.shape("square")
head.color("white")
head.penup()
head.goto(0, 0)
head.direction = "stop"
# Functions to control the snake's direction
def go_up():
if head.direction != "down":
head.direction = "up"
def go_down():
if head.direction != "up":
head.direction = "down"
def go_right():
if head.direction != "left":
head.direction = "right"
def go_left():
if head.direction != "right":
head.direction = "left"
# Function to move the snake
def move():
if head.direction == "up":
y = head.ycor()
head.sety(y + 20)
elif head.direction == "down":
y = head.ycor()
head.sety(y - 20)
elif head.direction == "right":
x = head.xcor()
head.setx(x + 20)
elif head.direction == "left":
x = head.xcor()
head.setx(x - 20)
# Function to create a button
def create_button(x, y, label, action):
button = turtle.Turtle()
button.speed(0)
button.shape("square")
button.color("gray")
button.shapesize(stretch_wid=2, stretch_len=4)
button.penup()
button.goto(x, y)
# Display the label on the button
label_turtle = turtle.Turtle()
label_turtle.speed(0)
label_turtle.color("white")
label_turtle.penup()
label_turtle.hideturtle()
label_turtle.goto(x, y - 10)
label_turtle.write(label, align="center", font=("Arial", 12, "bold"))
# Set button click action
button.onclick(lambda x, y: action())
# Create the directional control buttons on the screen
create_button(0, 200, "Up", go_up)
create_button(0, -200, "Down", go_down)
create_button(200, 0, "Right", go_right)
create_button(-200, 0, "Left", go_left)
# Main game loop
while True:
wn.update()
move()
ملف snake.py
# L7 [snake]
from turtle import Turtle
class Snake:
def __init__(self) :
self.turtles = []
self.positions = [ -40, -20, 0, 20, 40]
self.create_snake ()
def create_snake (self) :
for i in range(len(self.positions)) :
new_turtle = Turtle ("square")
new_turtle.color('white')
new_turtle.penup()
new_turtle.goto(i, 0)
self.turtles.append (new_turtle )
def move(self) :
for t in range(len(self.turtles)-1) :
self.turtles[t].goto(self.turtles[t+1].pos())
self.turtles[-1].forward(20)
# Function to create a button
def create_button(self, x, y, action):
button = Turtle()
button.speed(0)
button.shape("circle")
button.color("gray")
button.shapesize(stretch_wid=4, stretch_len=4)
button.penup()
button.goto(x, y)
# Set button action
button.onclick(lambda x, y: action())
def up(self) :
self.turtles[-1].setheading(90)
def down(self ) :
self.turtles[-1].setheading(270)
def left(self) :
self.turtles[-1].setheading (180)
def right(self) :
self.turtles[-1].setheading (0)
ملف main.py
# L7 [main]
import time
from turtle import Screen
from snake import Snake
window = Screen()
window.title("Keyboard control")
window.setup(600,1000)
window.bgcolor('black')
play_way = window.textinput("Choose play way", "Enter your way (buttons or keyboard)")
jack = Snake()
window.tracer(0)
if play_way == 'keyboard' :
game_on = True
while game_on :
jack.move()
window.update()
time.sleep(0.1)
window.listen()
window.onkey(jack.up, "Up" or "u")
window.onkey(jack.down, "Down" or "n")
window.onkey(jack.left, "Left" or "h")
window.onkey(jack.right, "Right" or "j")
elif play_way == "buttons" :
# Create on-screen control buttons
jack.create_button(0, -270 ,jack.up)
jack.create_button(0, -390 ,jack.down)
jack.create_button(60, -330 ,jack.right)
jack.create_button(-60, -330 ,jack.left)
game_on = True
while game_on :
jack.move()
window.update()
time.sleep(0.1)
window.exitonclick ()