هذا حل مشروع اللعبة الأخير الي طلبها الاستاذ ابراهيم عادل + تقييمكم وهل عندكم اقتراحات للتعديل أو التبسيط؟
main screen
from turtle import Turtle, Screen
from paddle import Paddle
from food import Food
from score_board import ScoreBoard
import random
import time
screen = Screen()
screen.setup(800,800)
screen.bgcolor('black')
screen.tracer(0)
paddle = Paddle()
food = Food()
score_board = ScoreBoard()
screen.update()
screen.onkey(paddle.move_right, 'Right')
screen.onkey(paddle.move_left, 'Left')
screen.listen()
def reset_food():
size = random.choice(food.size)
x_position = random.randint(-350,350)
food.shape(random.choice(food.shapes))
food.color(random.choice(food.colors))
food.shapesize(stretch_len= size , stretch_wid= size)
food.hideturtle()
food.goto(x_position,350)
food.showturtle()
game_on = True
while game_on:
reset_food()
while food.ycor() >= -400:
food.food_move()
screen.update()
time.sleep(0.1)
if food.ycor() <= -320 and food.distance(paddle) <= 80:
if food.shape() == 'circle':
score_board.increas_score(1)
elif food.shape() == 'square':
score_board.increas_score(2)
elif food.shape() == 'triangle':
score_board.clear()
score_board.score = 0
score_board.display_score()
elif food.shape() == 'turtle' and food.pencolor() != 'white':
score_board.increas_score(5)
else:
game_on = False
score_board.game_over()
break
break
screen.exitonclick()
paddle
from turtle import Turtle
class Paddle(Turtle):
def __init__(self):
super().__init__()
self.penup()
self.color('cyan')
self.shape('square')
self.shapesize(stretch_len=6, stretch_wid=1)
self.goto(0,-350)
def move_right(self):
self.goto(self.xcor()+40,self.ycor())
def move_left(self):
self.goto(self.xcor()-40,self.ycor())
food
from turtle import Turtle
import random
class Food(Turtle):
def __init__(self):
super().__init__()
self.penup()
self.colors = ['red','blue','white','yellow']
self.shapes = ['circle','turtle','square','triangle']
self.size = [1,1.5]
def food_move(self):
self.goto(self.xcor(), self.ycor()-20)
score board
from turtle import Turtle
class ScoreBoard(Turtle):
def __init__(self):
super().__init__()
self.penup()
self.color('white')
self.hideturtle()
self.score = 0
self.goto(0,350)
self.display_score()
def display_score(self):
self.write(f'Score: {self.score}', align='center', font=('courier',24,'bold'))
def increas_score(self, number):
self.clear()
self.score += number
self.display_score()
def game_over(self):
self.goto(0,0)
self.screen.bgcolor('#ff3399')
self.write(f'You lost! you ate the white turtle\nFainal Score: {self.score}', align='center', font=('arial',30,'bold'))