Python Project : Image Viewer App, WATER, GUN, OR SNAKE Game

 PYTHON PROJECTS:

reload ! diplomawaale.blogspot.com
Python game Project and GUI Application


DOWNLOAD SOURCE CODE CLICK HERE

(1). PYTHON GAME PROJECT( "WATER, GUN, OR SNAKE" ):

DESCRIPTION:

The 'import random' statement imports the 'random' module, which 'is used to generate random numbers.

The 'game win' function takes two arguments: 'computer' (representing the computer's choice) and you (representing your choice). It determines the winner based on the rules you provided for "water," "gun," and "snake."

The code generates a random number between 1 and 3 to determine the computer's choice. The computer's choice is assigned to the computer variable based on the random number.

The user is prompted to input their choice ("water," "gun," or "snake") using the input function, and the choice is stored in the you variable.

The game win function is called with the computer's choice and the user's choice to determine the game result.

The computer's and user's choices are displayed using the print function.

The game result (tie, the user wins, or the computer wins) is determined based on the return value of the game win function and is displayed using the print function.

SOURCE CODE:


import random

def gamewin (computer, you):
    if computer == you:
        return None
    elif computer == 's':
        if you == 'w':
            return False
        elif you == 'g':
            return True
    elif computer == 'w':
        if you == 'g':
            return False
        elif you == 's':
            return True
    elif computer == 'g':
        if you == 's':
            return False
        elif you == 'w':
            return True



print("Computer Turn: water(w)  gun(g) or snake(s)")
randno= random.randint(1, 3)
if randno == 1:
    computer = 'w'
elif randno == 2:
    computer = 'g'
elif randno == 3:
    computer = 's'

you = input("Your Turn: water(w) gun(g) or snake(s) \n ")
a = gamewin(computer,you)

print(f"computer choose {computer}")
print(f"you choose {you}")

if a==None:
    print("game tie")
elif a:
    print("you win")
else:
    print("you lose")



DOWNLOAD SOURCE CODE CLICK HERE

OUTPUT:

(2): IMAGE CHANGER AND SELECT FROM YOUR COMPUTER SOFTWARE ("GUI APPLICATION") :

DESCRIPTION:

  1. Importing Libraries:
    • Import the required libraries tkinter, file dialog from tkinter, and Image, ImageTk from PIL for GUI and image handling.
  2. Creating the Application Class:
    • Define a class named ImageChangerApp to manage the application.
  3. Initializing the GUI:
    • Set up the main window of the application and assign a title.
  4. Variables and Widgets:
    • Define variables to store the current image path and user's choice.
    • Create widgets including a label for displaying images, a button to choose images, a label for instructions, an option menu for image choices, and a button to change images.
  5. Defining Methods:
    • choose_image(self): Open a file dialog to select an image, display it on the label widget.
    • change_image(self): Change the displayed image based on the user's selected option.
  6. Main Application Loop:

§  In the if __name__ == "__main__": block:

§  Create the main tkinter window (root).

§  Initialize the ImageChangerApp instance with the main window.

§  Start the GUI event loop using root.mainloop().

 SOURCE CODE:


import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk

class ImageChangerApp:
def __init__(self, root):
self.root = root
self.root.title("Gaurav 1st executable software")

self.current_image_path = None
self.current_choice = None

self.image_label = tk.Label(root)
self.image_label.pack()

self.choose_button = tk.Button(root, text="CHOOSE A IMAGE FROM YOUR COMPUTER TO SHOW ON SOFTWARE", command=self.choose_image)
self.choose_button.pack()

self.choice_label = tk.Label(root, text="CHOOSE AN OPTION GIVEN BELOW :")
self.choice_label.pack()

self.choice_var = tk.StringVar()
self.choice_var.set("CHOOSE IMAGE ")
self.choice_menu = tk.OptionMenu(root, self.choice_var, "zeeshan image", "ashirwad image", "gaurav image")
self.choice_menu.pack()

self.change_button = tk.Button(root, text="Change Image", command=self.change_image)
self.change_button.pack()

def choose_image(self):
self.current_image_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.png *.jpg *.jpeg")])
if self.current_image_path:
image = Image.open(self.current_image_path)
image = image.resize((300, 300)) # Resize the image to fit the label
self.current_image = ImageTk.PhotoImage(image)
self.image_label.config(image=self.current_image)

def change_image(self):
self.current_choice = self.choice_var.get()
if self.current_choice == "zeeshan image":
new_image_path = r"C:\Users\madde\Desktop\python project\zeeshan.jpg"
elif self.current_choice == "ashirwad image":
new_image_path = r"C:\Users\madde\Desktop\python project\ashirwad.jpg"
elif self.current_choice == "gaurav image":
new_image_path = r"C:\Users\madde\Desktop\python project\zeeshan.jpg"
else:
new_image_path = None

if new_image_path:
image = Image.open(new_image_path)
image = image.resize((300, 300)) # Resize the image to fit the label
new_image = ImageTk.PhotoImage(image)
self.image_label.config(image=new_image)
self.image_label.image = new_image

if __name__ == "__main__":
root = tk.Tk()
app = ImageChangerApp(root)
root.mainloop()

Post a Comment

0 Comments