الكود :
إما تستعمل هذا الكود الموحد :
from turtle import Turtle,Screen
from time import sleep
from random import choice
class Bird ():
def __init__(self):
self.bird_parts = []
self.create_bird ()
def create_bird (self):
hair = Turtle ('circle')
hair.penup ()
hair.shapesize (5,4)
hair.setheading (270)
hair.color ('red')
hair.goto (-20,30)
self.bird_parts.append (hair)
body = Turtle ('circle')
body.penup ()
body.shapesize (5)
body.color ('yellow')
body.setheading (0)
self.bird_parts.append (body)
eye = Turtle ('circle')
eye.penup ()
eye.goto (20,20)
eye.shapesize (1,1,7)
eye.color ('white','black')
eye.setheading (0)
self.bird_parts.append (eye)
mouth = Turtle ()
mouth.penup ()
mouth.goto (20,-30)
mouth.shapesize (3,1)
mouth.setheading (270)
self.bird_parts.append (mouth)
nose = Turtle ()
nose.penup ()
nose.goto (70,0)
nose.shapesize (1,3)
nose.color ('orange')
self.bird_parts.append (nose)
def up (self):
for part in self.bird_parts:
part.goto (part.xcor(),part.ycor()+60)
def down (self):
for part in self.bird_parts:
part.goto (part.xcor(),part.ycor()-20)
class Walls ():
def __init__ (self):
self.wall1 = Turtle ('square')
self.wall2 = Turtle ('square')
self.init_walls1 ()
def init_walls1 (self):
self.len_wall1 = choice (range(5,90))
self.wall1.penup ()
self.wall1.color ('green')
self.wall1.setheading (180)
self.wall1.shapesize (self.len_wall1,5)
self.wall1.goto (500,-600)
self.len_wall2 = 90 - self.len_wall1
self.wall2.penup ()
self.wall2.color ('green')
self.wall2.setheading (180)
self.wall2.shapesize (self.len_wall2,5)
self.wall2.goto (500,600)
def move (self):
if self.wall1.xcor () > -500 :
self.wall1.forward (15)
self.wall2.forward (15)
else :
self.init_walls1 ()
window = Screen ()
window.title ('the bird game')
window.setup (600,1000)
window.bgcolor ('light blue')
window.tracer (0)
player = Bird ()
wall = Walls ()
window.update ()
def up (x,y):
player.up ()
window.onscreenclick (up)
def write_score (write,score):
write.clear ()
write.goto (0,300)
write.write (score,align='center',font = ('Ariel',50,'normal'))
game = True
score = 0
write = Turtle ()
write.penup ()
write.hideturtle ()
while game :
player.down ()
wall.move ()
if wall.wall1.xcor () in range (-100,100):
if player.bird_parts [1] .distance (wall.wall1) < wall.len_wall1 * 10 + 50 or player.bird_parts [1] .distance (wall.wall2) < wall.len_wall2 * 10 + 50:
game = False
if wall.wall1.xcor () == -100:
score += 1
if player.bird_parts [1].ycor () < -600 or player.bird_parts [1].ycor () > 600 :
game = False
write_score (write,score)
sleep (0.1)
window.update ()
window.clear ()
write.color ('red')
write.write (f'you lose with score : {score}',align='center',font=('Ariel',15,'normal'))
bird = Bird ()
window.exitonclick ()
او تستعمل هذه الاكواد المنفصله :
1-كود الطائر (الاعب)
from turtle import Turtle
class Bird ():
def __init__(self):
self.bird_parts = []
self.create_bird ()
def create_bird (self):
hair = Turtle ('circle')
hair.penup ()
hair.shapesize (5,4)
hair.setheading (270)
hair.color ('red')
hair.goto (-20,30)
self.bird_parts.append (hair)
body = Turtle ('circle')
body.penup ()
body.shapesize (5)
body.color ('yellow')
body.setheading (0)
self.bird_parts.append (body)
eye = Turtle ('circle')
eye.penup ()
eye.goto (20,20)
eye.shapesize (1,1,7)
eye.color ('white','black')
eye.setheading (0)
self.bird_parts.append (eye)
mouth = Turtle ()
mouth.penup ()
mouth.goto (20,-30)
mouth.shapesize (3,1)
mouth.setheading (270)
self.bird_parts.append (mouth)
nose = Turtle ()
nose.penup ()
nose.goto (70,0)
nose.shapesize (1,3)
nose.color ('orange')
self.bird_parts.append (nose)
def up (self):
for part in self.bird_parts:
part.goto (part.xcor(),part.ycor()+60)
def down (self):
for part in self.bird_parts:
part.goto (part.xcor(),part.ycor()-20)
2-كود العوائق (الجدران)
from turtle import Turtle
from random import choice
class Walls ():
def __init__ (self):
self.wall1 = Turtle ('square')
self.wall2 = Turtle ('square')
self.init_walls1 ()
def init_walls1 (self):
self.len_wall1 = choice (range(5,90))
self.wall1.penup ()
self.wall1.color ('green')
self.wall1.setheading (180)
self.wall1.shapesize (self.len_wall1,5)
self.wall1.goto (500,-600)
self.len_wall2 = 90 - self.len_wall1
self.wall2.penup ()
self.wall2.color ('green')
self.wall2.setheading (180)
self.wall2.shapesize (self.len_wall2,5)
self.wall2.goto (500,600)
def move (self):
if self.wall1.xcor () > -500 :
self.wall1.forward (15)
self.wall2.forward (15)
else :
self.init_walls1 ()
3-كود اللعب (الرئيسي)
from turtle import Turtle, Screen
from time import sleep
from bird import Bird
from walls import Walls
window = Screen ()
window.title ('the bird game')
window.setup (600,1000)
window.tracer (0)
window.bgcolor ('light blue')
player = Bird ()
wall = Walls ()
window.update ()
def up (x,y):
player.up ()
window.onscreenclick (up)
def write_score (write,score):
write.clear ()
write.goto (0,300)
write.write (score,align='center',font = ('Ariel',50,'normal'))
game = True
score = 0
write = Turtle ()
write.penup ()
write.hideturtle ()
while game :
player.down ()
wall.move ()
if wall.wall1.xcor () in range (-100,100):
if player.bird_parts [1] .distance (wall.wall1) < wall.len_wall1 * 10 + 50 or player.bird_parts [1] .distance (wall.wall2) < wall.len_wall2 * 10 + 50:
game = False
if wall.wall1.xcor () == -100:
score += 1
if player.bird_parts [1].ycor () < -600 or player.bird_parts [1].ycor () > 600 :
game = False
write_score (write,score)
sleep (0.1)
window.update ()
window.clear ()
write.color ('red')
write.write (f'you lose with score : {score}',align='center',font=('Ariel',15,'normal'))
bird = Bird ()
window.exitonclick ()
بعض الصور للعبه من تطبيق (pydroid3):