# بسم الله الرحمان الرحيم
from turtle import Screen, Turtle
import time, random
class HangmanGame:
def __init__(self,word,picturs):
# Initialize game state variables like lives, random_word, etc.
# Set up the turtle screen and graphics
# veraibols
self.word = word
self.image = 0
self.lives = 6
self.random_word = random.choice(self.word)
self.letters = []
self.space = ['_']*len(self.random_word)
self.picturs = picturs
# ----------------------------------------------------------------
# Turtles
self.pen = Turtle()
self.pen.hideturtle()
self.pen.penup()
self.pen.speed(100)
# Screen
self.see = Screen()
# ---------------------------------------------------------------------------
def update_screen(self,color,massage,go):
##Update the screen with a message and background color
self.see.clear()
self.see.bgcolor(color)
self.pen.goto(go[0],go[1])
self.pen.write(massage, font=('',40, 'bold'))
# --------------------------------------------------------------------------------
def handle_guess(self, letter):
# Check if the guessed letter is in the word
# Update masafat for all occurrences of the letter
# Manage lives and game state
if letter in self.letters:
self.update_screen(massage='You have chose this before',go=(-350,0),color='white') #agine
else:
if letter in self.random_word:
index = self.random_word.index(letter)
self.space[index] = letter
self.update_screen(massage='Correct guess!',color='green yellow',go=(-200,0)) #true
else:
self.letters.append(letter)
self.image += 1
self.lives -= 1
self.update_screen(massage=f'Wrong guess! {self.lives} lives left',color='red',go=(-300,0))
# -------------------------------------------------------------------------------------
def play(self):
time.sleep(2)
while "_" in self.space and self.lives > 0:
self.update_screen(massage='HANG MAN GAME',color='gray',go=(-200,250))
self.pen.goto(-600,60)
self.pen.write(self.picturs[self.image], font=('',28, "normal"))
self.pen.goto(x=-600,y=25)
self.pen.write(' '.join(self.space), font=('',24, "normal"))
self.pen.goto(x=-600,y=-20)
self.pen.write(f'You have {self.lives} lives', font=('',25, "normal"))
# input
letter = self.see.textinput(prompt='Gusse the leter', title='HANG MAN GAME')
self.handle_guess(letter)
time.sleep(1)
# Game over (win pr lose)
if self.lives > 0:
self.update_screen(massage='YOU WIN',color='green',go=(-100,0))
else:
self.update_screen(massage='YOU LOST',color='red',go=(-100,250))
self.pen.goto(-100,-200)
self.pen.write(self.picturs[self.image], font=('',28, "normal"))
# End game
self.see.exitonclick()
# -------------------------------------------------------------------------
HANGMANPICS = ['''
+---+
| |
|
|
|
|
=========''', '''
+---+
| |
O |
|
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
/|\ |
|
|
=========''', '''
+---+
| |
O |
/|\ |
/ |
|
=========''', '''
+---+
| |
O |
/|\ |
/ \ |
|
=========''']
# ---------------------------------------------------------------
wordlist = words = [
"apple", "banana", "cherry", "date",
"fig", "grape", "honeydew", "kiwi",
"lemon", "mango", "nectarine", "orange",
"papaya", "quince", "raspberry", "strawberry",
"tangerine", "ugli", "vanilla", "watermelon"
]
game = HangmanGame(wordlist,HANGMANPICS)
game.play()
6 إعجابات
مشاركة رائعة ومبتكرة في تطبيق ما تعلمته في الوحدات السابقة
شكرا لك على المشاركة الرائعة ابقى على هذا الطريق في تطوير مهاراتك البرمجية من خلال عمل مشاريع مخلتفة ومبتكرة ترسخ لك المفاهيم البرمجية
ومرحبا بك مجتمعنا
إعجاب واحد (1)
الله ينور ماشاء الله
إعجابَين (2)
جميل
زادكم الله علما
إعجاب واحد (1)
جميل ما شاء الله، زادكم الله توفيقاً وعلماً
إعجابَين (2)