السلام عليكم ورحمة الله وبركاته
لقد قمت بتطوير لعبة snake game التي تعلمنا كيف نصنعها مع الاستاذ ابراهيم جزاه الله خير الجزاء وانار الله بصيرته
اضفت للعبة شاشة بدء واشياء اخرى سا خبركم بها
لكن اولا لان عدد الاسطر كثيره نوعا ما سارفق كود main هناو الباقي في التعليقات
وصف سريع عن العبة:
1.لعبة منقسمه الى قسمين: وضع عادي ووضع المراحل
2.وضع المراحل (levels mod) يتكون من خمس مراحل متغيرة القوانين يعني في كل مرحله تجد شيء مختلف
3.الاسكور او انا اسميه في اللعبة(the target) لكل مرحله لها target خاص بها هذا خاص بال levels mod
4.الاسكور في الوضع العادي هو نفس الاسكور الموجود في اللعبة الاصل، يعني بيعد من صفر واحد… هكذا
5.big food وده اكل للثعبان لكن اكبر ويعطي نقاط اكثر
5. في وضع (levels mod) في 3 lives او حيوات في كل مرحله واذا خلصوا بتظهر رسالة لقد خسرت والسؤال هل تريد ان تلعب مجددا
6.في وضع ال(levelsmod) اذا اصدمت او خسرت حياه لا يعود ال(targe) الى صفر ويبدا من جديد بل يكون بنفس القيمه ما دام لم تنتهي كل الحيوات
- يوجد في الوضع العادي ميزه الجدران المفتوحه اي اذا اصتدمت باي جدار ترجع من الجهه المعاكسه وكذلك في بعض مراحل وضع levels mod
اما بخصوص الواجهه الخاصه باللعبة قبل بدء اللعب فيوجد التالي
1.start وهنا لبدء اللعبة
2.setting ويوجد به 4 خيارات
خيار لتغيير السرعه وخيار لتغيير الوضع الخاص باللعية وخيار الشكل الخاص بالثعبان والرجوع
ملحوظه: في المرحله رقم اربعه death ball بينتقل مكانها بعد ما تاكل 3 مرات.
هذا وصف سرع للعبة، مع اللعلم اللعبة ماذال ينقصها الكثير لهذا اريد منكم المشاركه وتعديل وان تشاركوني بابداعكم، كما ان هناك بعض الوظائف الموجوده باللعبة لم استخدمها بعد ، مثل last food التي بملف food وهي عباره عن طعام خاص يظهر في اخر اللعبة، لم اعرف كيف استخدمها لكن فكره خطرت ببالي ولم اطبقها بعد:joy:
اترك لكم الاضافه للتضيفوها بانفسكم وابدعوا…
هتلاقوا في الكود ده تركات جميله جدا ابرزها ازاي عملت فكرة target وازاي ميرجعش صفر بعد الخساره الا اذا الحيوات انتهت، وهتلاقو العقبات لها اشكال خطيره زي كرة الموت Death ball
وغيره وغيره وغيره هتلاقوه بي الكود، اتمني يعجبكم، وفي النهايه حابب اوجه كل امتنان واحترام للاستاذ ابراهيم صوفي المنشاء
ربنا يكرمك ويوفقك ويزيدك ثبات ونور ورضا وعز وشكرا.
ملف 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", 45, "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",60, "normal"))
window.update()
def Settings_window():
frame(bg="purple2",title="SETTINGS")
pen.goto(0, -330)
pen.write(f"1-{speed}\n\n2-{mood}\n\n3-{shape}\n\n4-BACK", align="center", font=("Courier", 50, "bold"))
window.update()
def shape_window():
frame(bg="purple2",title="SHAPE")
pen.goto(0, -200)
pen.write(f"1-square \n\n2-circle\n\n3-BACK", align="center", font=("Courier", 50, "bold"))
window.update()
def mood_window():
frame(bg="purple2",title="MOOD")
pen.goto(0, -250)
pen.write("1-Normal\n\n2-Levels\n\n3-Exit", align="center", font=("Courier",50, "bold"))
window.update()
def speed_window():
frame(bg="purple2",title="SPEED")
pen.goto(0, -330)
pen.write("1.Slow\n\n2.Normal\n\n3.Fast\n\n4.Vere Fast", align="center", font=("Courier", 50, "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", 70, "italic"))
if i >= 5:
pen.goto(0, 100) # تحديد موقع الكلمة تحت
pen.color(choice(text_colors))
pen.write("Congratulations!!!", align="center", font=("times new roman", 60, "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 -300 <= food2.xcor()<= -200 or 200 <= food2.xcor() <= 300) and (-280 <= food2.ycor() <= -230 or 230 <= 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_masseage():
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",50,"italic"))
pen.goto(0,-200)
pen.write("Click To Exit...",align="center",font=("arial",20,"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_masseage()
window.exitonclick()