<reponame>Caiocof/game_py
# Game Ping-Pong

from tkinter import *
import random
import time

level = int(input('which level you whant game? 1/2/3/4/5 \n'))
length = 500 / level

# DEFINE INTERFACE FOR GAME
root = Tk()
root.title('Ping Pong')
root.resizable(0, 0)
root.wm_attributes('-topmost', -1)

# RENDER INTERFACE
canvas = Canvas(root, width=800, height=600, bd=0, highlightthickness=0)
canvas.pack()

root.update()

# DEFINE VARIABLES FOR GAME
count = 0
lost = False


# CLASS INITIAL
class Ball:
    def __init__(self, canvas, Bar, color):
        self.canvas = canvas
        self.Bar = Bar
        self.id = canvas.create_oval(0, 0, 15, 15, fill=color)
        self.canvas.move(self.id, 245, 200)

        # DEFINE X AXIS
        starts_x = [-3, -2, -1, 1, 2, 3]
        random.shuffle(starts_x)

        self.x = starts_x[0]
        self.y = -3

        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()

    # FUNCTION WHICH DEFINE A DRAW
    def draw(self):
        self.canvas.move(self.id, self.x, self.y)

        # POSSITION INITIAL
        pos = self.canvas.coords(self.id)

        if pos[1] <= 0:
            self.y = 3

        if pos[3] >= self.canvas_height:
            self.y = -3

        if pos[0] <= 0:
            self.x = 3

        if pos[2] >= self.canvas_width:
            self.x = -3

        self.Bar_pos = self.canvas.coords(self.Bar.id)

        if pos[2] >= self.Bar_pos[0] and pos[0] <= self.Bar_pos[2]:
            if pos[3] >= self.Bar_pos[1] and pos[3] <= self.Bar_pos[3]:
                self.y = -3
                global count
                count += 1
                score()

        if pos[3] <= self.canvas_height:
            self.canvas.after(10, self.draw)
        else:
            game_over()
            global lost
            lost = True


class Bar:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0, 0, length, 10, fill=color)
        self.canvas.move(self.id, 200, 400)

        self.x = 0

        self.canvas_width = self.canvas.winfo_width()

        self.canvas.bind_all('<KeyPress-Left>', self.move_left)
        self.canvas.bind_all('<KeyPress-Right>', self.move_right)

    def draw(self):
        self.canvas.move(self.id, self.x, 0)

        self.pos = self.canvas.coords(self.id)

        if self.pos[0] <= 0:
            self.x = 0

        if self.pos[2] >= self.canvas_width:
            self.x = 0

        global lost

        if lost == False:
            self.canvas.after(10, self.draw)

    def move_left(self, event):
        if self.pos[0] >= 0:
            self.x = -3

    def move_right(self, event):
        if self.pos[2] <= self.canvas_width:
            self.x = 3


def start_game(event):
    global lost, count
    lost = False
    count = 0
    score()
    canvas.itemconfig(game, text=' ')

    time.sleep(1)
    Bar.draw()
    Ball.draw()


def score():
    canvas.itemconfig(score_now, text=f'Score: {count}')


def game_over():
    canvas.itemconfig(game, text='Game Over!')


Bar = Bar(canvas, '#EE7307')
Ball = Ball(canvas, Bar, '#0B22CC')

score_now = canvas.create_text(430, 20, text=f'Score: {count}', fill='#03AF06', font=('Arial', 16))
game = canvas.create_text(400, 300, text=' ', fill='#F10404', font=('Arial', 40))

canvas.bind_all('<Button-1>', start_game)
root.mainloop()

# Game Ping-Pong

from tkinter import *
import random
import time

level = int(input('which level you whant game? 1/2/3/4/5 \n'))
length = 500 / level

# DEFINE INTERFACE FOR GAME
root = Tk()
root.title('Ping Pong')
root.resizable(0, 0)
root.wm_attributes('-topmost', -1)

# RENDER INTERFACE
canvas = Canvas(root, width=800, height=600, bd=0, highlightthickness=0)
canvas.pack()

root.update()

# DEFINE VARIABLES FOR GAME
count = 0
lost = False


# CLASS INITIAL
class Ball:
    def __init__(self, canvas, Bar, color):
        self.canvas = canvas
        self.Bar = Bar
        self.id = canvas.create_oval(0, 0, 15, 15, fill=color)
        self.canvas.move(self.id, 245, 200)

        # DEFINE X AXIS
        starts_x = [-3, -2, -1, 1, 2, 3]
        random.shuffle(starts_x)

        self.x = starts_x[0]
        self.y = -3

        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()

    # FUNCTION WHICH DEFINE A DRAW
    def draw(self):
        self.canvas.move(self.id, self.x, self.y)

        # POSSITION INITIAL
        pos = self.canvas.coords(self.id)

        if pos[1] <= 0:
            self.y = 3

        if pos[3] >= self.canvas_height:
            self.y = -3

        if pos[0] <= 0:
            self.x = 3

        if pos[2] >= self.canvas_width:
            self.x = -3

        self.Bar_pos = self.canvas.coords(self.Bar.id)

        if pos[2] >= self.Bar_pos[0] and pos[0] <= self.Bar_pos[2]:
            if pos[3] >= self.Bar_pos[1] and pos[3] <= self.Bar_pos[3]:
                self.y = -3
                global count
                count += 1
                score()

        if pos[3] <= self.canvas_height:
            self.canvas.after(10, self.draw)
        else:
            game_over()
            global lost
            lost = True


class Bar:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0, 0, length, 10, fill=color)
        self.canvas.move(self.id, 200, 400)

        self.x = 0

        self.canvas_width = self.canvas.winfo_width()

        self.canvas.bind_all('<KeyPress-Left>', self.move_left)
        self.canvas.bind_all('<KeyPress-Right>', self.move_right)

    def draw(self):
        self.canvas.move(self.id, self.x, 0)

        self.pos = self.canvas.coords(self.id)

        if self.pos[0] <= 0:
            self.x = 0

        if self.pos[2] >= self.canvas_width:
            self.x = 0

        global lost

        if lost == False:
            self.canvas.after(10, self.draw)

    def move_left(self, event):
        if self.pos[0] >= 0:
            self.x = -3

    def move_right(self, event):
        if self.pos[2] <= self.canvas_width:
            self.x = 3


def start_game(event):
    global lost, count
    lost = False
    count = 0
    score()
    canvas.itemconfig(game, text=' ')

    time.sleep(1)
    Bar.draw()
    Ball.draw()


def score():
    canvas.itemconfig(score_now, text=f'Score: {count}')


def game_over():
    canvas.itemconfig(game, text='Game Over!')


Bar = Bar(canvas, '#EE7307')
Ball = Ball(canvas, Bar, '#0B22CC')

score_now = canvas.create_text(430, 20, text=f'Score: {count}', fill='#03AF06', font=('Arial', 16))
game = canvas.create_text(400, 300, text=' ', fill='#F10404', font=('Arial', 40))

canvas.bind_all('<Button-1>', start_game)
root.mainloop()
# Game Ping-Pong # DEFINE INTERFACE FOR GAME # RENDER INTERFACE # DEFINE VARIABLES FOR GAME # CLASS INITIAL # DEFINE X AXIS # FUNCTION WHICH DEFINE A DRAW # POSSITION INITIAL