CodeErorsNo / Coder

Code for apk
0 stars 0 forks source link

snakegamepy #1

Open CodeErorsNo opened 1 year ago

CodeErorsNo commented 1 year ago

import pygame from tkinter import * from PIL import Image, ImageTk import random import time import webbrowser

root = Tk() image = Image.open("ZanFon.jpg") image = image.resize((722, 1300)) photo = ImageTk.PhotoImage(image)

background_label = Label(root, image=photo) background_label.place(x=0, y=0, relwidth=1, relheight=1)

def sait(): webbrowser.open("https://chat.openai.com")

class SnakeGame: def init(self): self.root = Tk() self.root.title("Игра Змейка") self.root.geometry("690x1250") self.root["bg"] = "black" self.menu = Menu(self.root) self.root.config(menu=self.menu) def colo_white(): self.root.configure(bg="white") def colo_black(): self.root.configure(bg="black") self.game_menu = Menu(self.menu)

    self.menu.add_cascade(label="Кнопка                                                                                                                  ", menu=self.game_menu, background="blue")
    self.game_menu.add_command(label="Играть                                                          ", command=self.start_game)
    self.game_menu.add_separator()
    self.game_menu.add_command(label="Выход                                                         ", command=self.root.quit)
    btn1 = Button(self.root, text="Играть",  command=self.start_game, bd=50, width=40, bg="black", fg="white", activebackground="red")
    btn1.pack()
    btn_nezbayu_kakoy = Button(self.root, text="Изминит музыку", bd=50, width=40, bg="black", fg="white", activebackground="red", command=Music)
    btn_nezbayu_kakoy.pack()
    btn = Button(self.root, text="Белый фон", bd=50, width=40, bg="black", fg="white", activebackground="red", command=colo_white)
    btn_black = Button(self.root, text="Чёрный фон", bd=50, width=40, bg="black", fg="white", activebackground="red", command=colo_black)
    btn_black.pack()
    btn.pack()

    btn_lox_bot_mne_nodoelo = Button(self.root, text="Назад", bd=50, width=40, bg="black", fg="white", activebackground="red", command=self.root.destroy)
    btn_lox_bot_mne_nodoelo.pack()
    self.root.mainloop()

def start_game(self):
    self.game_window = Toplevel(self.root)
    self.game_window.title("Игра Змейка")
    self.game_window.geometry("690x1270")
    self.game_window["bg"] = "black"

    self.canvas = Canvas(self.game_window, width=678, height=901, borderwidth=5, relief="ridge")
    self.canvas.pack()

    self.snake = [(100, 100), (80, 100), (80, 100)]
    self.food = self.spawn_food()
    self.direction = "Right"
    self.game_over = False

    self.up_button = Button(self.game_window, text="Вверх", command=self.move_up, bd=23, bg="orange")
    self.down_button = Button(self.game_window, text="Вниз", command=self.move_down, bd=23, bg="pink")
    self.left_button = Button(self.game_window, text="Влево", command=self.move_left, bd=33, bg="blue")
    self.right_button = Button(self.game_window, text="Вправо", command=self.move_right, bd=23, bg="lime")
    self.close = Button(self.game_window, text="Назад", command=self.game_window.destroy, bd=8, bg="red")

    self.close.place(x=500, y=1190)
    self.play_again_button = Button(self.game_window, text="заново", command=self.restart_game, bg="olive")
    self.up_button.pack(side="top")
    self.down_button.pack(side="bottom")
    self.left_button.pack(side="left")
    self.right_button.pack(side="right")
    self.play_again_button.pack()
    self.play_again_button.pack_forget()

    self.update_game()

def spawn_food(self):
    while True:
        x = random.randint(1, 66) * 10
        y = random.randint(1, 86) * 10
        if (x, y) not in self.snake:
            return x, y

def move(self):
    if self.game_over:
        return

    head_x, head_y = self.snake[0]
    new_head = (head_x, head_y)

    if self.direction == "Up":
        new_head = (head_x, head_y - 10)
    elif self.direction == "Down":
        new_head = (head_x, head_y + 10)
    elif self.direction == "Left":
        new_head = (head_x - 10, head_y)
    elif self.direction == "Right":
        new_head = (head_x + 10, head_y)

    if new_head[0] < 0 or new_head[0] >= 679 or new_head[1] < 0 or new_head[1] >= 909:
        self.game_over = True
        self.canvas.create_text(400, 200, text="Проиграл!", font=("Helvetica", 24))
        self.play_again_button.pack()
        return

    self.snake.insert(0, new_head)

    if new_head == self.food:
        self.food = self.spawn_food()
    else:
        self.snake.pop()

    self.check_collision()

    self.update_game()

def check_collision(self):
    head = self.snake[0]
    for segment in self.snake[1:]:
        if head == segment:
            self.game_over = True
            self.canvas.create_text(400, 200, text="Проиграл!", font=("Helvetica", 24))
            self.play_again_button.pack()
            return

def update_game(self):
    self.canvas.delete("all")

    if not self.game_over:
        for segment in self.snake:
            x, y = segment
            self.canvas.create_rectangle(x, y, x + 10, y + 10, fill="green")

        fx, fy = self.food
        self.canvas.create_oval(fx, fy, fx + 10, fy + 10, fill="red")

        self.game_window.after(100, self.move)

def move_up(self):
    if self.direction != "Down":
        self.direction = "Up"

def move_down(self):
    if self.direction != "Up":
        self.direction = "Down"

def move_left(self):
    if self.direction != "Right":
        self.direction = "Left"

def move_right(self):
    if self.direction != "Left":
        self.direction = "Right"

def restart_game(self):
    self.snake = [(100, 100), (90, 100), (80, 100)]
    self.food = self.spawn_food()
    self.direction = "Right"
    self.game_over = False
    self.play_again_button.pack_forget()
    self.update_game()

def Games(): game = SnakeGame()

def play_music(): pygame.mixer.music.load("Pigstep.mp3") pygame.mixer.music.play()

def stop_music(): pygame.mixer.music.stop()

def cvaz(): settings_window = Toplevel(root)

settings_window.title("Связь")
settings_window.geometry("690x1600")
label = Label(settings_window, text="Привет! \nЕсли хочешь связаться со мной,\nвот как это можно сделать \nНомер телефона - \n+996755440540 \nЭлектронная почта - \nalibekvakibaev6@gmail.com\n3. Телеграм - @McBoxAdminBota\nПоддержка материально на \nномер телефона", fg="white", bg="red", font=("Arial 14", 10))
label.pack()
settings_window["bg"] = "red"

close_button2 = Button(settings_window, text="Закрыть", command=settings_window.destroy, bd=50, width=40, bg="black", fg="white", activebackground="red")
close_button2.pack()

pygame.init()

def open_settings(): settings_window = Toplevel(root) settings_window.title("Настройки") settings_window.geometry("690x1270") settings_window["bg"] = "red"

play_button = Button(settings_window, text="Воспроизвести музыку", command=play_music, bd=50, width=40, bg="black", fg="white", activebackground="red")
play_button.pack()
stop_button = Button(settings_window, text="Остановить музыку", command=stop_music, bd=50, width=40, bg="black", fg="white", activebackground="red")
stop_button.pack()

background_color_button3 = Button(settings_window, text="Связь с нами", bd=50, width=40, bg="black", fg="white", command=cvaz, activebackground="red")
background_color_button3.pack()

button_sait = Button(settings_window, text="Наш сайт", command=sait, bd=50, width=40, bg="black", fg="white", activebackground="red")
button_sait.pack()

button1 = Button(settings_window, text="Изминит музыку", bd=50, width=40, bg="black", fg="white", activebackground="red", command=Music)
button1.pack()

close_button = Button(settings_window, text="Закрыть", command=settings_window.destroy, bd=50, width=40, bg="black", fg="white", activebackground="red")
close_button.pack()

def randi():

nicknames = ["Joins", "homik", "dink", "posetk1", "amogs", "islam", "Aruuke", "rafhan", "islam", "alibek", "joodarbek", "salmurbek", "abuvacir", "raxima", "Aruujan", "Master009", "login", "Snake", "random", "BossSnake", "Kamila", "Altinay", "Daniel", "Muslim", "Nursaid", "Krasafchik", "Botik", "I_M_Master", "Matrix", "Voxel", "Vortex", "Blitz", "Byte", "Comet", "Digital", "Flash", "Glitch", "Hacker", "Infinite", "Jolt", "Laser", "Matrix", "Nanobot", "Orbit", "Pixel", "Quasar", "Robot", "Spark", "Titan", "Uplink", "Viral", "Wave", "Xeno", "Yotta", "Zeus", "Alpha", "Beta", "Gamma", "Delta", "Echo", "Foxtrot", "Giga", "Hotel", "India", "Juliet", "Kilo", "Lima", "Mike", "Nova", "Omega", "Pulse", "Quark", "Rogue", "Sigma", "Tango", "Urban", "Vector", "Warp", "X-Ray", "Yacht", "Zero", "Aeon", "Bolt", "Cipher", "Drone", "Exo", "Fusion", "Galaxy", "Holo", "Ion", "Jinx", "Kaiju", "Lunar", "Meta", "Neon", "Orca", "Plasma", "Quint", "Rapid", "Sonic", "Tron", "Ultraviolet", "Vortex", "Warp", "Xenon", "Yonder", "Zenith", "Atom", "Blaze", "Cosmo", "Dimension", "Echo", "Flare", "Gamma", "Haven", "Ignite", "Jolt", "Kairos", "Lumen", "Mystic", "Nebula", "Onyx", "Photon", "Quantum", "Radiant", "Solar", "Terra", "Unity", "Voyage", "Wavelength", "Xerxes", "Yugen", "Zephyr", "Astral", "Bloom", "Celestial", "Dynamo", "Eclipse", "Fathom", "Glide", "Harmony", "Infinity", "Jade", "Kinesis", "Lunar", "Mirage", "Nimbus", "Oracle", "Pandora", "Quest", "Radiance", "Sable", "Tempest", "Umbra", "Vesper", "Wraith", "Xylia", "Yara", ]
random_nickname = random.choice(nicknames)
entry.delete(0, END)
entry.insert(0, random_nickname)

def exit_app(): root.destroy()

def fik(): settings_window = Toplevel(root) settings_window.title("Загадка") settings_window.geometry("690x1270") label2 = Label(settings_window, text="Игра создано Алибекам", fg="white", bd=20, bg='black') label2.pack() settings_window["bg"] = "red" close_buttonk = Button(settings_window, text="Закрыть", command=settings_window.destroy, bd=50, width=40, bg="black", fg="white", activebackground="red") close_buttonk.pack()

photik = ImageTk.PhotoImage(file="posetk1.png")

pygame.init()

btn1 = Button(text="Играть", bg="yellow", bd=10, width=473, height=70, activebackground="red", command=Games, image=photik) btn1.pack()

def save_entry_value(): with open("entry_value.txt", "w") as f: f.write(entry.get())

save = "" try: with open("entry_value.txt", "r") as f: save = f.read() except FileNotFoundError: pass

entry = Entry(root, bd=12) entry.insert(0, save) entry.place(x=125, y=440) btn1.place(x=100, y=520)

btn_save = Button(text="Сахраник ник", bg="yellow", bd=10, width=20, height=-30, activebackground="red", command=save_entry_value) btn_save.place(x=100, y=720)

btn2 = Button(text="Настройки", bg="yellow", bd=10, width=20, height=-30, activebackground="red", command=open_settings) btn2.place(x=100, y=620)

btn3 = Button(text="Выйти", bg="yellow", bd=10, width=20, height=-30, command=exit_app, activebackground="red") btn3.pack() btn3.place(x=100, y=820)

btn = Button(text="Рандомное имя", command=randi, activebackground="red") btn.place(x=353, y=362) labelk = Label(text='Введите имя:', fg="red") labelk.pack() labelk.place(x=123, y=380)

label30 = Label(text="Зм", fg="blue", bg="yellow", font="Arial 14") label30.place(x=120, y=260)

label31 = Label(text="ей", fg="lime", bg="yellow", font="Arial 14") label31.place(x=201, y=260)

label32 = Label(text="ка", fg="red", bg="yellow", font="Arial 14") label32.place(x=275, y=260)

label33 = Button(text="Jon", fg="lime", bg="black", font="Arial 14", command=fik) label33.place(x=350, y=260) def play_music7(): settings_window = Toplevel(root) settings_window.title("Инструкция") settings_window.geometry("500x700") label = Label(settings_window, text="Внимание\nПодождите 4-5 секунд\n что бы музыка начелас", fg="red") label.pack() close_button = Button(settings_window, text="Закрыть", command=settings_window.destroy, bd=50, width=40, bg="black", fg="white", activebackground="red") close_button.pack() pygame.mixer.music.load("Shum.mp3") pygame.mixer.music.play()

def play_music6(): settings_window = Toplevel(root) settings_window.title("Инструкция") settings_window.geometry("500x700") label = Label(settings_window, text="Внимание\nПодождите 4-5 секунд\n что бы музыка начелас", fg="red") label.pack() close_button = Button(settings_window, text="Закрыть", command=settings_window.destroy, bd=50, width=40, bg="black", fg="white", activebackground="red") close_button.pack() pygame.mixer.music.load("Wolni.mp3") pygame.mixer.music.play()

def play_music5(): settings_window = Toplevel(root) settings_window.title("Инструкция") settings_window.geometry("500x700") label = Label(settings_window, text="Внимание\nПодождите 4-5 секунд\n что бы музыка начелас", fg="red") label.pack() close_button = Button(settings_window, text="Закрыть", command=settings_window.destroy, bd=50, width=40, bg="black", fg="white", activebackground="red") close_button.pack() pygame.mixer.music.load("IsFor.mp3") pygame.mixer.music.play()

def play_music4(): settings_window = Toplevel(root) settings_window.title("Инструкция") settings_window.geometry("500x700") label = Label(settings_window, text="Внимание\nПодождите 4-5 секунд\n что бы музыка начелас", fg="red") label.pack() close_button = Button(settings_window, text="Закрыть", command=settings_window.destroy, bd=50, width=40, bg="black", fg="white", activebackground="red") close_button.pack() pygame.mixer.music.load("Pigstep.mp3") pygame.mixer.music.play()

def play_music3(): settings_window = Toplevel(root) settings_window.title("Инструкция") settings_window.geometry("500x700") label = Label(settings_window, text="Внимание\nПодождите 4-5 секунд\n что бы музыка начелас", fg="red") label.pack() close_button = Button(settings_window, text="Закрыть", command=settings_window.destroy, bd=50, width=40, bg="black", fg="white", activebackground="red") close_button.pack() pygame.mixer.music.load("BadBoy.mp3") pygame.mixer.music.play()

def play_music2(): settings_window = Toplevel(root) settings_window.title("Инструкция") settings_window.geometry("500x700") label = Label(settings_window, text="Внимание\nПодождите 4-5 секунд\n что бы музыка начелас", fg="red") label.pack() close_button = Button(settings_window, text="Закрыть", command=settings_window.destroy, bd=50, width=40, bg="black", fg="white", activebackground="red") close_button.pack() pygame.mixer.music.load("Minecraft.mp3") pygame.mixer.music.play() def play_music8(): settings_window = Toplevel(root) settings_window.title("Инструкция") settings_window.geometry("500x700") label = Label(settings_window, text="Внимание\nПодождите 4-5 секунд\n что бы музыка начелас", fg="red") label.pack() close_button = Button(settings_window, text="Закрыть", command=settings_window.destroy, bd=50, width=40, bg="black", fg="white", activebackground="red") close_button.pack() pygame.mixer.music.load("maga.mp3") pygame.mixer.music.play()

def Music(): okno = Toplevel(root) okno.title("Музыки") okno.geometry("690x1600")

music = Button(okno, text="Clasik", width=40, height=-30, activebackground="red", bg="green", command=play_music8)
music.pack()
music = Button(okno, text="Волны", width=40, height=-30, activebackground="red", bg="lime", command=play_music6)
music.pack()
music2 = Button(okno, text="Шум", width=40, height=-30, activebackground="red", bg="blue", command=play_music7)
music2.pack()
music3 = Button(okno, text="Pigstep", bd=10, width=40, height=-30, activebackground="red", bg="brown", command=play_music4)
music3.pack() 
music4 = Button(okno, text="BodBay", bd=10, width=40, height=-30, activebackground="red", bg="yellow", command=play_music3)
music4.pack() 
music5 = Button(okno, text="Minecraft", bd=10, width=40, height=-30, activebackground="red", bg="Pink", command=play_music2)
music5.pack()
music6 = Button(okno, text="Пианино", bd=10, width=40, height=-30, activebackground="red", bg="green", command=play_music5)
music6.pack()
music_dis = Button(okno, text="Остановить музыку", bd=50, width=40, height=-30, activebackground="red", bg="black", fg="White", command=stop_music)
music_dis.pack()

close_button = Button(okno, text="Закрыть", command=okno.destroy, bd=50, width=40, bg="black", fg="white", activebackground="red")
close_button.pack()

def Com(): roots = Toplevel(root) roots.geometry("690x1270") roots.title("Учимся") roots["bg"] = "black"

label = Label(roots, text="Предложение курс пайтон\nделается она пока сырая но\nскоро выйдет", fg="white", bg="black")
label.pack()

btn1 = Button(roots, text="Назад", bg="lime", fg="red", width=40, height=3, activebackground="red", command=roots.destroy) 
btn1.pack(side="bottom")

def zm(): settings_window = Toplevel(root) settings_window.title("Нейросет") settings_window.geometry("690x1600")

main_menu = Menu() file_menu = Menu() file_menu.add_command(label="Настроки ", command=open_settings, font="Arial 14", activebackground="red") file_menu.add_command(label="Остановит музыку ", font="Arial 14", activebackground="red", command=stop_music) file_menu.add_command(label="Изминит Музыку ", font="Arial 14", activebackground="red", command=Music) file_menu.add_command(label="Курсы пайтон ", font="Arial 14", activebackground="red", command=Com) main_menu.add_cascade(label="Функции (это кнопка) ", menu=file_menu, font="Arial 14", activebackground="lime", background="lime") root.config (menu=main_menu)

root.mainloop() posetk1 PSX_20230809_190930

CodeErorsNo commented 1 year ago

Game tkinter