اذهب إلى المحتوى
  • 0

برمجة لعبة بلغة بايثون ...أريد مساعدة بسيطة

المهند سالم

السؤال

أريد أفكار للعبة بسيطة بلغة بايثون بدون أن أستخدم  المكتبات  PyGame أو غيرها ...

مطلوب( إبداع + ابتكار) و أمامي يوم فقط .

('أنا في ورررطه')print

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 1

مرحباً بك صديقي @المهند سالم .

يمكنك عمل لعبة من غير Pygame   , مثلا تستعمل print()  في لعبة tictactoe أو أن تخمين الرقم الصحيح.

مثال , هذه اللعبة : 
 

'''
لعبة الأبقار والثيران ، هي لعبة نقوم بتوليد رقم عشوائي من 4 أرقام ، بينما يخمن المستخدم
 رقم مكون من 4 أرقام ، إذا حصل على التخمين الصحيح مع المكان الصحيح للرقم الذي يحصل عليه + 1 بقرة ،
 إذا حصل على التخمين الصحيح ولكن ليس في المكان المناسب سيحصل على +1 ثور.
 إذا حصل عليهم كلهم صحيحين سوف تنتهي اللعبة!
'''

def GameLoop():
    LogFile = "CowsAndBulls.log"
    if os.path.isfile(LogFile) == True:
        file = open(LogFile,"w").close()
        
    logging.basicConfig(filename = LogFile, level = logging.INFO, format = "%(levelname)s |\
    %(asctime)s | Line Number : %(lineno)d | %(message)s")

    logging.info("--------------------GAME STARTED--------------------")
    
    i = string.digits
    numbers = [x for x in i]
    Cow = 0
    Bull = 0
    logging.info("Cow variable , Bull variable are created <type int> value : 0")

    print("""
    ----------Welcome to Cows and Bulls game----------
    4 digits number is generated , put your guess,
    for each correct guess with right place +1 cow,
    for each correct guess with wrong place +1 bull,
    Guess it all correctly to Win, or type exit to exit! :)
    """)


    while True:
        
        #======================================================
        # Generating Random numbers
        random.shuffle(numbers)
        game = []
        
        for i in range(4):
            game.append(numbers[i])
            
        logging.info(f"GameLoop generated a 4 digit number : {game[0]}{game[1]}{game[2]}{game[3]}")
        
        #======================================================
        # User input handling
        check = input("Enter your guess :\n ")
        
        try:
            if check.lower() == "exit":
                print("\n")
                print("Game Over , Your Score is... ")
                time.sleep(3)
                print(f"Cows : {Cow}")
                print(f"Bulls : {Bull}")
                logging.info("Your typed exit, loops breaks")
                logging.info(f"Results , Cows={Cow} , Bulls={Bull}")
                
                break
            if check.isdigit() == False:
                raise Exception("Please enter digits characters!")
        except Exception as err:
            print(f"Error occured ,{err}")
            logging.error(f"An error had been raised and handled : {err}")
            continue
        
        guess = [s for s in check[0:4]]
        
        logging.info(f"User 4 digit guess is : {guess[0]}{guess[1]}{guess[2]}{guess[3]}")
        
        #======================================================
        # Game Logic
        x = 0
        if game == guess:
            print("\n")
            Cow += 4
            print("You Guessed it! , Your Score is... ")
            time.sleep(3)
            print(f"Cows : {Cow}")
            print(f"Bulls : {Bull}")
            logging.info(f"Results , Cows={Cow} , Bulls={Bull}")
            logging.info(f"User guessed it all correct , Loop breaked!")
            break
        else:
            for i in game:
                if i == guess[x]:
                    Cow += 1
                
                if i in guess:
                    Bull += 1
                x += 1
                
                
        logging.info(f"Results , Cows={Cow} , Bulls={Bull}")
        
        print("Generated Numbers: ")
        print(game)
        print("Your Guess : ")
        print(guess)
        print(f"Cows = {Cow}")
        print(f"Bulls = {Bull}")
        print("\n")
        
        logging.info("Loop continuing to the next >>")

        
if __name__ == "__main__":
    import random
    import string
    import time
    import sys
    import logging
    import os
    GameLoop()

    

 

باستخدام print() ، يمكنك إنشاء العديد من الألعاب مثل hangman , أو لعبة أسئلة و المستخدم يختار الجواب الصحيح.

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 1

راااااائع ,,,,أنت مذهل ...

شكرا لك ...هل يمكنك أن تكتب لي برمجة hangman إذا سمحت؟؟؟أحتاجها

هنا في هذه اللعبة فوق هل قمت بتحميل صور أو ملف ؟؟بحيث أحتاج إلى تغييرها ..لو أمكنك أن تشرح لي ما كتبته فلم أفهم كل البرمجة و بعضها فهمتها . أي فقط ماذا فعلت في كل خطوة ك هاشتاق. فقط لو أمكنك .

آسف على إتعابك .

شكرا مرة أخرى على مبادرتك , آسف جدا على إزعاجك.

إذا أمكنك أن توضح لي شكل الصور التي استخدمها كي أقوم بتنزيلها لدي و تشرح لي البرمجة ...الصور التي يجب أن أستخدمها لدي يجب أن تكون Pnj أو jpg

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

مرحباً صديقي @المهند سالم.

اللعبة لا  تستخدم صورة , فقط نرسل النتائج من خلال print() , هنا شرح الكود :
 

#! python3

# Cows and Bulls game, its a game that we generate a random 4 digit number, while the user guesses
# a 4 digit number, if he get a right guess with the right place of the number he gets + 1 cow,
# if he gets right guess but not at right place he gets + 1 bull.
# if he got them all right then game finishes!


# هنا function اللعبة
def GameLoop():

    ######### (ليست مهمة)  هي مكتبة متخصصة لإرسال معلومة عن تطور البرنامج وقت التشغيل  logging هنا نستعمل   
    LogFile = "CowsAndBulls.log"
    if os.path.isfile(LogFile) == True: # إذا لم يمكن موجود logging ننشئ ملف من أجل ال
        file = open(LogFile,"w").close()
        
        # logging خيارات ال 
    logging.basicConfig(filename = LogFile, level = logging.INFO, format = "%(levelname)s |\
    %(asctime)s | Line Number : %(lineno)d | %(message)s")
#############

    logging.info("--------------------GAME STARTED--------------------") # نرسل إلى ملف "CowsAndBulls.log" معلومة أن العبة بدأت.
    
    i = string.digits # '0123456789' القيمة  i و نعطي string  هنا نستخدم مكتبة 
    numbers = [x for x in i] # numbers = ['0','1','2','3','4','5','6','7','8','9']
    Cow = 0
    Bull = 0
    logging.info("Cow variable , Bull variable are created <type int> value : 0")

    print("""
    ----------Welcome to Cows and Bulls game----------
    4 digits number is generated , put your guess,
    for each correct guess with right place +1 cow,
    for each correct guess with wrong place +1 bull,
    Guess it all correctly to Win, or type exit to exit! :)
    """)


    while True:
        
        #======================================================
        # Generating Random numbers
        random.shuffle(numbers) # عشوائي numbers تغير ترتيب 
        game = []
        
        for i in range(4):
            game.append(numbers[i]) # game نعين قمية من أربع أرقام عشوائية إلى 
            
        logging.info(f"GameLoop generated a 4 digit number : {game[0]}{game[1]}{game[2]}{game[3]}")





        
        #======================================================
        # User input handling
        check = input("Enter your guess :\n ") #   المستخدم يدخل قيمة
        
        try:
            if check.lower() == "exit": # نرسل النتيجة إلى المستخدم و تنتهي اللعبة exit إذا كانت القيمة 
                print("\n")
                print("Game Over , Your Score is... ")
                time.sleep(3)
                print(f"Cows : {Cow}")
                print(f"Bulls : {Bull}")
                logging.info("Your typed exit, loops breaks")
                logging.info(f"Results , Cows={Cow} , Bulls={Bull}")
                
                break
            if check.isdigit() == False: # إذا لم تكن القيمة رقم , نطلب إعادة إدخال قيمة
                raise Exception("Please enter digits characters!")
        except Exception as err:
            print(f"Error occured ,{err}")
            logging.error(f"An error had been raised and handled : {err}")
            continue
        
        guess = [s for s in check[0:4]] # list أرقام المستخدم قي 
        
        logging.info(f"User 4 digit guess is : {guess[0]}{guess[1]}{guess[2]}{guess[3]}")



        
        #======================================================
        # Game Logic
        x = 0
        if game == guess: # %إذا كان جواب المستخدم صحيح 100
            print("\n")
            Cow += 4
            print("You Guessed it! , Your Score is... ")
            time.sleep(3)
            print(f"Cows : {Cow}")
            print(f"Bulls : {Bull}")
            logging.info(f"Results , Cows={Cow} , Bulls={Bull}")
            logging.info(f"User guessed it all correct , Loop breaked!")
            break 
        else:  # غير ذلك نجمع نقاط الأرقام الصحيحة
            for i in game:
                if i == guess[x]:
                    Cow += 1
                
                if i in guess:
                    Bull += 1
                x += 1




                
        logging.info(f"Results , Cows={Cow} , Bulls={Bull}")
        
        print("Generated Numbers: ")
        print(game)
        print("Your Guess : ")
        print(guess)
        print(f"Cows = {Cow}")
        print(f"Bulls = {Bull}")
        print("\n")
        
        logging.info("Loop continuing to the next >>")

        
if __name__ == "__main__":
    import random
    import string
    import time
    import sys
    import logging
    import os
    GameLoop()

    



 

أما عن لعبة hangman :


import time


name = input("What is your name? ")

print("Hello, " + name, "Time to play hangman!")

print("")


time.sleep(1)

print("Start guessing...")
time.sleep(0.5)

#الكلمة السرية للعبة
word = "secret"

#متفير فارغ من أجل قمية المستخدم
guesses = ''

#عدد المحولات الممكنة للمستخدم
turns = 10




while turns > 0:         

   
    failed = 0             

    # لأي حرف في الكلمة السرية     
    for char in word:      

    # إذا كان هنالك حرف صحيح قي قيمة المستخدم
        if char in guesses:    
    
        # نطبع الإجابة الصحيحة
            print(char)   

        else:
    
        # 
            print("_")    
       
        # غير ذالك نزيد على عدد المحولات الفاشلة
            failed += 1    

    
    if failed == 0:         
        print("You won")  

    # exit the script
        break              

    print()

    
    guess = input("guess a character:") 


    guesses += guess                    

    #  إذا كان الجواب خطأ
    if guess not in word:  
 
        turns -= 1        
        print("Wrong")    
 
    
        print("You have", + turns, 'more guesses') 
 
    # المستخدم إذاإكتملت عدد المحولات يخسر
        if turns == 0:           
    
            print("You Lose")  

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

مرحبا بكم جميعا

احتاج افكار مبدعة و جديدة بعض الشي .. لألعاب اقةم بتصميمها و برمجتها بنفسي ، شرط ان تكون بلغة بايثون .. ولكم مني جزيل الشكر

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...