main.py
from turtle import Screen, Turtle
from paddle_M import Paddle
from score import Sscorboard
from rain import My_turtles
import time
import random
screen = Screen()
screen.tracer(0)
screen.setup(800, 600)
screen.bgcolor(‘black’)
screen.title(“OctuCode: My Game”)
paddle = Paddle()
score = Sscorboard()
my_turtle = My_turtles()
screen.listen()
screen.onkey(paddle.go_left, ‘Left’)
screen.onkey(paddle.go_right, ‘Right’)
دالة لتوليد قيمة عشوائية جديدة للموقع x
def random_x():
return random.randint(-360, 360)
default_sleep = 0.1
game_on = True
while game_on:
my_turtle.goto(random_x(), 290) # استخدم الدالة random_x لتوليد موقع عشوائي
screen.update()
time.sleep(default_sleep)
my_turtle.sety(my_turtle.ycor() - 10)
if my_turtle.ycor() < -300:
if my_turtle.shape() == 'circle':
score.point(1)
default_sleep *= 0.9
if my_turtle.shape() == 'square':
score.point(2)
default_sleep *= 0.9
if my_turtle.shape() == 'triangle':
score.reset()
default_sleep *= 0.9
if my_turtle.shape() == 'turtle' and my_turtle.color() == 'white':
score.game_over()
# بعد سقوط السلحفاة، يتم إعادة تموضعها في الأعلى مرة أخرى
my_turtle.goto(random_x(), 290) # استخدم الدالة random_x لتوليد موقع عشوائي
time.sleep(default_sleep)
score.py
from turtle import Turtle , Screen
screen = Screen()
class Sscorboard(Turtle):
def init(self):
super().init()
self.color(“white”)
self.hideturtle()
self.penup()
self.score = 0
self.update_score()
def point(self,x):
self.score += x
self.update_score()
def reset(self):
self.score =0
self.update_score()
def update_score(self):
self.clear()
self.goto(0,250)
self.write(self.score, align='center', font=('courier', 40, 'normal'))
def game_over(self):
self.screen.bgcolor("darkred")
self.goto(0,0)
self.write(f"Game Over \nFinal Score: {self.score}", align="center", font=("Arial", 30, "normal"))
rain.py
from turtle import Turtle
import random
shapes = ‘classic’, ‘turtle’, ‘triangle’, ‘classic’, ‘arrow’, ‘square’, ‘circle’
colors = ‘black’, ‘blue’, ‘green’, ‘red’, ‘yellow’, ‘purple’, ‘pink’, ‘orange’, ‘white’, ‘gray’, ‘cyan’
width = random.randint(1,3) # Random width between 1 and 10
length = random.randint(1,3) # Random length between 1 and 10
outline = random.randint(1,2) # Random outline thickness between 1 and 5
class My_turtles(Turtle):
def init(self):
super().init()
self.color(random.choice(colors))
self.shape(random.choice(shapes))
self.shapesize(stretch_wid=width, stretch_len=length, outline=outline)
self.penup()
self.setheading(270)
paddle.py
from turtle import Turtle
class Paddle(Turtle):
def init(self, position):
super().init()
self.shape(“square”)
self.color(“white”)
self.penup()
self.goto(position)
self.shapesize(stretch_wid=5,stretch_len=1)
def go_left(self):
self.goto(self.xcor() - 40, self.ycor())
def go_right(self):
self.goto(self.xcor() + 40, self.ycor())