السلام عليكم ورحمة الله وبركاته
تم تعديل مشروعي للعبة الثعبان
the snakegame(Achieve the goal)
لتناسب الهاتف، لكن اولا ان لم تكن تعلم ما هي هذه اللعبة،
فقد نشرت هذه اللعبة من قبل وهي مناسبه للكمبيوتر ستجدها في الرابط التالي: Snake game (achieve the goal) -levels mod & normal mod
ذكرت كل شيء عن اللعبة…
الان قمت بتعديل اللعبة لتناسب الهاتف، وتعمل على برنامج pydroid 3، لكن هناك مشكلة في واجهة pydroid وهي انها لا يمكن تصغيرها او التعديل عليها.، لكن استاذ عادل اكتشف حل المشكله، يمكنكم رؤية ذلك في الرابط التالي: وأخيرااا حل مشكلة أبعاد الشاشة على تطبيق pydroid3 جزاك الله خيراً يا استاذ عادل، بعد ما تطبق الخطوات، شغل الكود وابداء اللعبه اثناء عمل اللعبه او حركة الثعبان اسحب النافذة فوق قليلا لان الكيبورد سيغطي جزء منها لذا ارفع النافذه لاعلى
ى.
، ملحوظة: ازرار التحكم في اللعبة ستكون من الارقام وهي كالاتي: فوق: 2، تحت: 8، يمين: 6، شمال: 4
واخيرا اريد ان ان اجدد شكري للاستاذ @ibrahim
main.py
from turtle import Turtle, Screen
from snake import Snake
from food import Food
from lives import Lives
from big_food import Bigfood
from score import scoreboard
from score_normal import scoreboard_normal
from barrier import Barrier
from random import choice
import time
window = Screen()
window.tracer(0)
window.title('Snake Game By "MoHorus"')
window.setup(height=800,width=700)
pen = Turtle()
pen.hideturtle()
pen.penup()
pen.speed('fastest')
text_colors = ["gold", "limegreen", "deepskyblue", "hotpink", "orange", "yellow", "cyan"]
bg_colors = ["purple2", "black", "midnightblue", "darkslateblue", "darkorchid"]
special_nums=[4,7,11,15,19,20]
num = 0
level=1
snake_speed=0.1
snake_shape="square"
shape="Shape >Square"
speed= "Speed >Normal"
mood = "Mood >Levels"
def frame(bg, title):
window.tracer(0)
window.bgcolor(bg)
pen.clear()
pen.color("black")
pen.penup()
pen.goto(0, 250)
pen.write(title, align="center", font=("arial", 18, "bold")) # حجم خط أصغر
pen.goto(-351, 230)
pen.pendown()
pen.pensize(15)
for _ in range(4):
pen.fd(700)
pen.lt(90)
pen.fd(110)
pen.lt(90)
pen.penup()
def start_window():
frame(bg="gold", title="Snake Game")
pen.goto(0, -250)
pen.write("1.Start\n\n2.Sitting\n\n3.Exit", align="center", font=("times new roman", 20, "normal")) # حجم خط أصغر
window.update()
def Settings_window():
frame(bg="purple2", title="SETTINGS")
pen.goto(0, -400)
pen.write(f"1-{speed}\n\n2-{mood}\n\n3-{shape}\n\n4-BACK", align="center", font=("Courier", 20, "bold")) # حجم خط أصغر
window.update()
def shape_window():
frame(bg="purple2", title="SHAPE")
pen.goto(0, -250)
pen.write(f"1-square \n\n2-circle\n\n3-BACK", align="center", font=("Courier", 20, "bold")) # حجم خط أصغر
window.update()
def mood_window():
frame(bg="purple2", title="MOOD")
pen.goto(0, -270)
pen.write("1-Normal\n\n2-Levels\n\n3-Exit", align="center", font=("Courier", 20, "bold")) # حجم خط أصغر
window.update()
def speed_window():
frame(bg="purple2", title="SPEED")
pen.goto(0, -400)
pen.write("1.Slow\n\n2.Normal\n\n3.Fast\n\n4.Vere Fast", align="center", font=("Courier", 20, "bold")) # حجم خط أصغر
window.update()
def winner():
window.clear()
window.tracer(0)
for i in range(10): # عدد المرات اللي هيتغير فيها الألوان
window.bgcolor(choice(bg_colors)) # تغيير خلفية الشاشة
pen.clear()
pen.color(choice(text_colors))
pen.goto(0, 200)
pen.write("You win", align="center", font=("times new roman", 25, "italic")) # حجم خط أصغر
if i >= 5:
pen.goto(0, 100) # تحديد موقع الكلمة تحت
pen.color(choice(text_colors))
pen.write("Congratulations!!!", align="center", font=("times new roman", 18, "italic")) # حجم خط أصغر
window.update() # تحديث الشاشة
time.sleep(0.7) # تأخير بسيط لتأثير
def next_level_window():
next_level=window.textinput("You Win....", "Now there is only one level left in this game \n\nAt this level the snake's speed will be normal\n\nDid you want to go next level? \n1.yes\n2.no" if level == 4 else 'did you want to go next level?\n1.yes\n2.no')
while True:
if next_level:
if next_level.lower()=="y" or next_level.lower()=="yes" or next_level.lower()=="1":
return True
elif next_level.lower()=='n' or next_level.lower() == "no" or next_level.lower()=="2" :
window.clear()
return False
else:
next_level=window.textinput("Type Error...",'Please write (y or n)\n\nDid you want to go next level?\n1.yes\n2.no')
else:
next_level=window.textinput("No Value",'Please Type your choice...\n\nDid you want to go next level?\n1.yes\n2.no')
def reset_all(target_score):
global num
global target
num=0
window.clear()
window.tracer(0)
the_snake.Reset()
food2.Reset()
food.Reset()
food2.apper()
live_snake.loss_of_live()
if level >= 2:
barrier1.Reset()
barrier2.Reset()
barrier1.square()
barrier2.square()
if level >= 4:
barrier3.Reset()
barrier3.death_ball()
if level == 5:
barrier2.x=barrier1.x
barrier2.y=barrier1.y
barrier2.death_ball()
score.Reset()
if live_snake.num_lives <= 0:
score.target = 0
if live_snake.num_lives > 0 :
score.target = target_score
score.real_target = target
score.update_score()
def show_lose_window():
global num
global target
the_snake.snake_died()
reset_all(target - (target - score.target))
num = 0
if live_snake.num_lives == 0:
score.lose()
window.update()
playagain = window.textinput("You Lose", 'Did you want to try again?\n1.yes\n2.no')
while True:
if playagain:
if playagain.lower() in ['y','n','yes','no','1','2']:
if playagain.lower()== 'n' or playagain.lower()== 'no' or playagain== '2':
window.clear()
window.update()
return False
else:
reset_all(target)
live_snake.Reset()
return True
else:
playagain = window.textinput("wait....", f'>{playagain}< this is not in choices\nPlease Write (y or n)\n\nDid you want to try again?\n1.yes\n2.no')
else:
playagain = window.textinput("wait....", "Please Type your choice...... \n\nDid you want to try again?\n1.yes\n2.no)")
return True
def the_game():
global num
global change_pos
global target
n=0
timer=0
game = True
while game:
the_snake.move()
window.update()
for i in range(len(the_snake.turtles)-1):
if the_snake.head.distance(the_snake.turtles[i])<19:
game = show_lose_window()
break
if the_snake.head.distance(food2) < 19:
if level == 4:
food2.apper_normalmood()
timer +=1
else:
food2.apper()
score.increase_score()
the_snake.new_segment()
num+=1
if num in special_nums:
food.apper()
if the_snake.head.distance(food) <24:
food.hide_food()
score.great_increase_score()
if score.target >= target:
live_snake.num_lives = 0
food2.hideturtle()
the_snake.hidesnake()
score.win()
window.update()
return True
if level ==1 or level==2:
if the_snake.head.xcor() < -330 or the_snake.head.xcor() > 330 or the_snake.head.ycor() < -340 or the_snake.head.ycor() > 300:
game = show_lose_window()
if level == 2:
if the_snake.head.distance(barrier1)<170 and (-180 <= the_snake.head.xcor() <= 210 and -100 <= the_snake.head.ycor() <= -90 ):
game = show_lose_window()
if the_snake.head.distance(barrier2)<170 and (-180 <= the_snake.head.xcor() <= 210 and 80 <= the_snake.head.ycor() <= 120) :
game = show_lose_window()
if (-180 <= food2.xcor() <= 210) and (-100 <= food2.ycor() <= -90 or 80 <= food2.ycor() <= 120):
food2.apper()
elif level ==3:
if the_snake.head.xcor() <=-350 or the_snake.head.xcor() >= 350:
the_snake.head.goto(the_snake.head.xcor()*-1,the_snake.head.ycor())
n+=1
if n>=3:
the_snake.head.goto(-330,the_snake.head.ycor())
n=0
if the_snake.head.ycor() < -330 or the_snake.head.ycor() > 280:
game = show_lose_window()
if the_snake.head.distance(barrier1) <150 and (230 <=the_snake.head.xcor() <=250 and -200 <= the_snake.head.ycor()<= 100) :
game = show_lose_window()
if the_snake.head.distance(barrier2) <150 and (-260 <=the_snake.head.xcor() <=-240 and -200 <= the_snake.head.ycor()<= 100) :
game = show_lose_window()
if (-255 <=food2.xcor() <=-230 or 230 <= food2.xcor()<= 255) and -200 <= food2.ycor() <= 100 :
food2.apper()
elif level ==4:
barrier3.lt(15)
if the_snake.head.ycor() <= -280 or the_snake.head.ycor() >= 280:
the_snake.head.goto(the_snake.head.xcor(),(the_snake.head.ycor()*-1))
n+=1
timer +=1
if n>=5:
if the_snake.head.xcor() >= 280:
the_snake.head.goto((the_snake.head.xcor()*-1)-40,the_snake.head.ycor()-40)
if the_snake.head.xcor() <= -280:
the_snake.head.goto((the_snake.head.xcor()*-1)-40,the_snake.head.ycor()+40)
n=0
if the_snake.head.xcor() < -330 or the_snake.head.xcor() > 330:
game = show_lose_window()
if the_snake.head.distance(barrier1) <150 and (-90 <=the_snake.head.xcor() <=220 and -280 <= the_snake.head.ycor()<= -230) :
game = show_lose_window()
if the_snake.head.distance(barrier2) <150 and (-90 <=the_snake.head.xcor() <=220 and 230 <= the_snake.head.ycor()<= 280) :
game = show_lose_window()
if the_snake.head.distance(barrier3)<70 and ((-300<=the_snake.head.xcor()<=-200 or 200<=the_snake.head.xcor()<=300) and -50<=the_snake.head.ycor()<=50):
game = show_lose_window()
if (-90 <=food2.xcor() <=-220 or -270 <= food2.xcor()<= -200 or 200 <= food2.xcor() <= 270) and (-280 <= food2.ycor() <= -210 or 210 <= food2.ycor() <= 280 or -50 <= food2.ycor() <= 50) :
food2.apper_normalmood()
if timer >= 3:
change_pos *= -1
barrier3.change(change_pos,0,5,5)
timer=0
else:
timer += 1
barrier1.monster()
barrier2.x += 40
barrier2.goto(barrier2.x,barrier2.y)
barrier2.death_ball()
barrier3.lt(20)
if barrier2.x >= 300:
barrier2.hideturtle()
barrier2.Circle.hideturtle()
barrier2.x= barrier1.x - 20
barrier2.y = barrier1.y
if timer == 5:
barrier3.y *= -1
barrier3.death_ball()
if timer >=20:
barrier1.y -=80
if barrier1.y <-300:
barrier1.y = 270
timer=0
if the_snake.head.xcor() < -330 or the_snake.head.xcor() > 330 or the_snake.head.ycor() < -340 or the_snake.head.ycor() > 280:
game = show_lose_window()
if the_snake.head.distance(barrier2)<50 or the_snake.head.distance(barrier2.Circle) <50:
game = show_lose_window()
if the_snake.head.distance(barrier3) <= 50 or the_snake.head.distance(barrier3.Circle) <= 50:
game = show_lose_window()
if food2.distance(barrier3) <= 50 or food2.distance(barrier3.Circle) <= 50:
food2.apper()
window.update()
time.sleep(snake_speed)
return False
def the_game_normal():
global num
n = 0
game_on=True
while game_on:
the_snake.move()
window.update()
time.sleep(snake_speed)
for i in range(len(the_snake.turtles)-1):
if the_snake.head.distance(the_snake.turtles[i])<19:
score2.lose()
window.update()
time.sleep(5)
game_on=False
break
if the_snake.head.distance(food2) < 19:
food2.apper_normalmood()
score2.increase_score()
the_snake.new_segment()
num+=1
if num in special_nums:
food.apper()
if the_snake.head.distance(food) < 22:
food.hide_food()
the_snake.new_segment()
score2.great_increase_score()
if the_snake.head.xcor() <= -340 or the_snake.head.xcor() >= 340 :
the_snake.head.goto(the_snake.head.xcor()*-1,the_snake.head.ycor())
num=0
n+=1
if n>=3:
if the_snake.head.xcor() >= 340:
the_snake.head.goto((the_snake.head.xcor()*-1)-40,the_snake.head.ycor()-40)
if the_snake.head.xcor() <= -340:
the_snake.head.goto((the_snake.head.xcor()*-1)-40,the_snake.head.ycor()+40)
n=0
if the_snake.head.ycor() <= -280 or the_snake.head.ycor() >= 280:
the_snake.head.goto((the_snake.head.xcor()),(the_snake.head.ycor()*-1))
n+=1
if n>=3:
the_snake.head.goto((the_snake.head.xcor(),270))
n=0
def exit_message():
window.clear()
window.bgcolor('black')
pen.goto(0, 100)
pen.color("white")
pen.write("This Game Created \n By MoHorus", align="center", font=("times new roman", 15, "italic")) # تصغير حجم الخط
pen.goto(0, -200)
pen.write("Click To Exit...", align="center", font=("arial", 10, "bold")) # تصغير حجم الخط
game_on = True
content_for_menu_message = ("snake game", "choice 1-3")
while game_on :
window.clear()
start_window()
menu = window.textinput(content_for_menu_message[0], content_for_menu_message[1])
if menu:
if menu.isdigit():
if menu == "1":
start_play= True
while start_play:
pen.clear()
food2=Food()
food=Bigfood()
if level == 0:
score2= scoreboard_normal()
food2.apper_normalmood()
the_snake=Snake("white",level,snake_shape)
the_game_normal()
start_play=False
else:
target = 13
score=scoreboard(target= target)
level=1
snake_color="green2"
the_snake=Snake(snake_color,level,snake_shape)
live_snake=Lives(snake_color)
if the_game():
if next_level_window():
target = 17
snake_color="blue2"
level = 2
the_snake = Snake(snake_color,level,snake_shape)
barrier1=Barrier(50,-100,16,1)
barrier2=Barrier(50,100,16,1)
reset_all(target)
live_snake.clear()
live_snake=Lives(snake_color)
if the_game():
if next_level_window():
target = 23
snake_color="white"
level =3
the_snake = Snake(snake_color,level,snake_shape)
barrier1.change(250,-50,1,16)
barrier2.change(-250,-50,1,16)
reset_all(target)
live_snake.clear()
live_snake=Lives(snake_color)
if the_game():
if next_level_window():
target = 25
snake_color="orange2"
level =4
the_snake = Snake(snake_color,level,snake_shape)
change_pos=250
barrier3=Barrier(change_pos,0,5,5)
barrier3.death_ball()
barrier1.change(50,-250,16,2)
barrier2.change(50,250,16,2)
reset_all(target)
live_snake.clear()
live_snake=Lives(snake_color)
if the_game():
if next_level_window():
target = 20 #لان بصراحة المرحلة دي صعبه جدا ، كنت عايز 30 بس كده هتكون صعبه جدا والهدف اصلا ان اللعبة تكون ممتعة (^_^)
snake_speed= 0.1
snake_color="blue2"
level =5
the_snake = Snake(snake_color,level,snake_shape)
barrier1.change(-300,250,1,10)
barrier2.change(-250,250,5,3)
barrier3.change(250,200,5,5)
reset_all(target)
live_snake.clear()
live_snake=Lives(snake_color)
if the_game():
winner()
time.sleep(5)
start_play=False
elif menu == "2":
content_for_settings_message=("Settings", "Choice 1-4")
while True:
Settings_window()
settings_choice = window.textinput(content_for_settings_message[0],content_for_settings_message[1])
if settings_choice:
if settings_choice.isdigit():
if settings_choice == "1":
content_for_speed_message = ("Speed", "Choice 1-4\n\nor write * to go back")
while True :
speed_window()
speed_choice = window.textinput(content_for_speed_message[0],content_for_speed_message[1])
if speed_choice:
if speed_choice=="*" or speed_choice.isdigit():
if speed_choice in ["1","2","3","4","*"]:
if speed_choice == "1":
speed= "Speed >Slow"
snake_speed=0.2
if speed_choice == "2":
speed= "Speed >Normal"
snake_speed=0.1
if speed_choice == "3":
speed= "Speed >Fast"
snake_speed=0.06
if speed_choice=='4':
speed= "Speed >Very Fast"
snake_speed=0.03
break
else:
content_for_speed_message = ("Choice out","Your choice is out the list\n\nchoice 1-4 \n\nor write * to go back")
else:
content_for_speed_message = ("Choice error",f"Wrong entry: {speed_choice}\nPlease enter a number only \nchoice 1-4 \n\nor write * to go back")
else:
content_for_speed_message = ("no value","Please enter your choice \n\nchoice 1-4 \n\nor write * to go back")
elif settings_choice == "2":
content_for_mood_message = ("mood","choice 1-3")
while True:
mood_window()
mood_choice = window.textinput(content_for_mood_message[0],content_for_mood_message[1])
if mood_choice:
if mood_choice.isdigit():
if mood_choice in ["1","2","3"]:
if mood_choice =="1":
level=0
mood= "Mood >Normal"
if mood_choice == "2":
level=1
mood= "Mood >Levels"
break
else:
content_for_mood_message = ("Choice out","Your choice is out the list\n\nchoice 1-3 ")
else:
content_for_mood_message = ("Choice error",f"Wrong entry: {mood_choice}\nPlease enter a number only \nchoice 1-3 ")
else:
content_for_mood_message = ("no value","Please enter your choice \n\nchoice 1-3 ")
elif settings_choice =="3":
content_for_shape_message = ("shape","choice 1-3")
while True:
shape_window()
shape_choice = window.textinput(content_for_shape_message[0],content_for_shape_message[1])
if shape_choice:
if shape_choice.isdigit():
if shape_choice in ["1","2","3"]:
if shape_choice =="1":
snake_shape="square"
shape= "Shape >Square"
if shape_choice == "2":
snake_shape="circle"
shape= "Shape >Circle"
break
else:
content_for_shape_message = ("Choice error","Your choice is out the list\n\nchoice 1-3 ")
else:
content_for_shape_message = ("Choice error",f"Wrong entry: {shape_choice}\nPlease enter a number only \nchoice 1-3 ")
else:
content_for_shape_message = ("no value","Please enter your choice \n\nchoice 1-3 ")
elif settings_choice == "4":
pen.clear()
break
else:
content_for_settings_message =("Choice out","Your choice is out the list\n\nchoice 1-3 ")
else:
content_for_settings_message = ("Choice error",f"Wrong entry: {settings_choice}\nPlease enter a number only \nchoice 1-3 ")
else:
content_for_settings_message = ("no value","Please enter your choice \n\nchoice 1-3 ")
elif menu == "3":
game_on=False
else:
content_for_menu_message=("Choice out","Your choice is out the list\n\nchoice 1-3 ")
else:
content_for_menu_message=("Choice error",f"Wrong entry: {menu}\nPlease enter a number only \nchoice 1-3 ")
else:
content_for_menu_message=("no value","Please enter your choice \n\nchoice 1-3 ")
window.update()
exit_message()
window.exitonclick()