Check out my new python game Leave ur comment when u guys try it out

+8 Divulger Michealson · April 25, 2016
from tkinter import*
import random
import time

counter = 0
counter1 = 0

tk = Tk()
tk.title("Ball Game")

tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width = 500, height = 400, bd = 0, highlightthickness = 0)
canvas.config(bg = "red")
canvas.pack()
tk.update()

canvas.create_line(250,0,250,400, fill = 'blue')

class Ball:
    
    def __init__(self,canvas,paddle,paddle1,color):
        self.canvas = canvas
        self.paddle = paddle
        self.paddle1 = paddle1
        self.id = canvas.create_oval(10,10,25,25,fill = color)
        self.canvas.move(self.id,233,200)
        starts = [-3,3]
        random.shuffle(starts)
        self.x = starts[0]
        self.y = -3
        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()
        
        
    def draw(self):
        
        self.canvas.move(self.id, self.x ,self.y)
        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
            self.score(True)
        if pos[2] >= self.canvas_width:
            self.x = -3
            self.score(False)
        if self.hit_paddle(pos) == True:
            self.x = 3
        if self.hit_paddle1(pos) == True:
            self.x = -3
            
    def hit_paddle(self,pos):
        paddle_pos = self.canvas.coords(self.paddle.id)

        if pos[1] >= paddle_pos[1] and pos[1] <= paddle_pos[3]:
            if pos[0]>= paddle_pos[0] and pos[0] <= paddle_pos[2]:
                return True
            return False

    def hit_paddle1(self,pos):
        paddle_pos = self.canvas.coords(self.paddle1.id)

        if pos[1] >= paddle_pos[1] and pos[1] <= paddle_pos[3]:
            if pos[2]>= paddle_pos[0] and pos[2] <= paddle_pos[2]:
                return True
            return False
    
    def score(self, val):
        global counter
        global counter1

        if val == True:

            a = self.canvas.create_text(125,40,text = counter, font = ("Cursive",60), fill = "white")
            canvas.itemconfig(a, fill = "red")
            counter += 1
            a = self.canvas.create_text(125,40,text = counter, font = ("Cursive",60), fill = "white")

        if val == False:

            a = self.canvas.create_text(375,40,text = counter1, font = ("Cursive",60), fill = "gold")
            canvas.itemconfig(a, fill = "red")
            counter1 += 1
            a = self.canvas.create_text(375,40,text = counter1, font = ("Cursive",60), fill = "gold")
            
class Paddle:
    
    def __init__(self,canvas,color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0,150,20,250, fill = color)
        self.y = 0
        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()
        self.canvas.bind_all('w', self.turn_left)
        self.canvas.bind_all('s', self.turn_right)
        
        
    def draw(self):
        self.canvas.move(self.id, 0, self.y)
        pos = self.canvas.coords(self.id)

        if pos[1] <= 0:
            self.y = 0
        if pos[3] >= 400:
            self.y = 0
    def turn_left(self,evt):
        self.y = -3

    def turn_right(self,evt):
        self.y = 3

class Paddle1:

    def __init__(self,canvas,color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(470,150,500,250, fill = color)
        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()
        self.y = 0
        self.canvas.bind_all('<KeyPress-Up>', self.turn_left)
        self.canvas.bind_all('<KeyPress-Down>', self.turn_right)
        

    def draw(self):
        self.canvas.move(self.id, 0, self.y)
        pos = self.canvas.coords(self.id)

        if pos[1] <= 0:
            self.y = 0
        if pos[3] >= 400:
            self.y = 0
    def turn_left(self,evt):
        self.y = -3

    def turn_right(self,evt):
        self.y = 3

        
paddle = Paddle(canvas, "yellow")
paddle1 = Paddle1(canvas,"blue")
ball = Ball(canvas,paddle,paddle1, "green")

while 1:
    
    ball.draw()
    paddle.draw()
    paddle1.draw()
    
    if counter ==10:
        ball.x =0
        ball.y = 0
        paddle.y = 0
        paddle1.y = 0
        
        canvas.create_text(250,200,text = "Congrats Player 1! Wins", font = 32, fill = "white")
        canvas.create_text(250,215,text = "Score: "+ str(counter) + " - " + str(counter1), font = 32, fill = "silver")
        canvas.create_text(250,150,text = "Game OVER!!!", font = ("cursive",50), fill = "gray")
        
    if counter1 ==10:
        ball.x =0
        ball.y = 0
        paddle.y = 0
        paddle1.y = 0
        
        canvas.create_text(250,200,text = "Congrats Player 2! Wins", font = 32, fill = "black")
        canvas.create_text(250,215,text = "Score: " + str(counter) + " - " + str(counter1), font = 32, fill = "yellow")
        canvas.create_text(250,150,text = "Game OVER!!!", font = ("cursive",50), fill = "black")

        tk.update_idletasks()
    tk.update()
    time.sleep(0.012)

    if counter == 10 or counter1 == 10:
        time.sleep(100000)

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
+1 Sampan Verma · April 25, 2016
It is pretty good. You can definitely improve the Design. But still good game.
+1 john mickman · April 26, 2016
how to check this game i mean how to play this game?
0 Divulger Michealson · April 26, 2016
lol arrow key up down

and key w s

that how you play this game
0 Divulger Michealson · April 26, 2016
taking pythont to the next level lol time to start using it as an AI i mean i started programming last year this same time am impressed i mean C,python,Jquery,Css3,Html5,Javascript, php i have conquered them all but tbh forget how to do a few thing i think i am going to settle down with Python,Php,C and Jquery
0 Sampan Verma · April 28, 2016
Why settle for C?
0 Divulger Michealson · April 29, 2016
am in the process of cpp but C i just dont know it just my first language as in the first one i choose to learn
to me it was pretty straight forward but i just want to have it seen that i do alot of networking it will come in handy one day altho python can do the job have to give a lil respect to C 
0 Arkz Dark · May 1, 2016
hello, i really want to see the game but I am noob when it comes to programming. I have python 2.7.11 installed on my windows 10. I want to ask you if I copy this code and save it in .py file and convert it with Py2exe into .exe file, will it work or there is different procedure? thanks.
0 Divulger Michealson · May 7, 2016
Well you could but i recommend you get an upg to the python 3 it has more rules but u gotta work with it.
0 * cwgt · May 11, 2016
good. but it has a problem when score becomes 10, i am not able to exit it on ubuntu!
0 ariel bitan · May 11, 2016
nice game
  • 1
  • 2

Python

128,034 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator