OrangoMango

PongTutorial-3

May 16th, 2020
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. from tkinter import *
  2. import time, random
  3.  
  4. class Game:
  5.     def __init__(self):
  6.         self.tk = Tk()
  7.         self.tk.title("Pong tutorial")
  8.         self.tk.resizable(0,0)
  9.         self.tk.wm_attributes("-topmost", 1)
  10.         self.canvas = Canvas(self.tk, width=500, height=500)
  11.         self.canvas.pack()
  12.         self.score = 0
  13.         self.scoretext = self.canvas.create_text(0, 0, anchor="nw", text="Score: {0}".format(self.score), font="Purisa 20 bold", fill="red")
  14.     def mainloop(self):
  15.         while True:
  16.             b.draw()
  17.             p.draw()
  18.             self.tk.update()
  19.             time.sleep(0.01)
  20.        
  21. class Ball:
  22.     def __init__(self, game, paddle):
  23.         self.tk = game
  24.         self.id = self.tk.canvas.create_oval(20, 20, 38, 38, fill="red")
  25.         l = [-3, -2, -1, 1, 2, 3]
  26.         x = random.choice(l)
  27.         self.x, self.y = x,-3
  28.         self.paddle = paddle
  29.     def draw(self):
  30.         self.tk.canvas.move(self.id, self.x, self.y)
  31.         pos = self.tk.canvas.coords(self.id) #[x, y, x1, y1]
  32.         if pos[0] <= 0: #x
  33.             self.x = 3
  34.         elif pos[1] <= 0: #y
  35.             self.y = 3
  36.         elif pos[2] >= 500: #x1
  37.             self.x = -3
  38.         elif pos[3] >= 500: #y1
  39.             self.y = 0 #GAME OVER
  40.             self.x = 0
  41.         elif self.hit_paddle():
  42.             self.tk.score += 1
  43.             self.tk.canvas.itemconfig(self.tk.scoretext, text="Score: {0}".format(self.tk.score))
  44.             self.tk.tk.update()
  45.             self.y = -3
  46.     def hit_paddle(self):
  47.         pos = self.tk.canvas.coords(self.id)
  48.         paddle_pos = self.tk.canvas.coords(self.paddle.id)
  49.         if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]: #collision between x axis
  50.             if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]: #collision between y axis
  51.                 return True
  52.         return False
  53.  
  54. class Paddle:
  55.     def __init__(self, game):
  56.         self.tk = game
  57.         self.id = self.tk.canvas.create_rectangle(0, 0, 100, 10, fill="blue")
  58.         self.tk.canvas.move(self.id, 200, 400)
  59.         self.x, self.y = 0, 0
  60.         self.tk.canvas.bind_all("<KeyPress-Right>", self.right)
  61.         self.tk.canvas.bind_all("<KeyPress-Left>", self.left)
  62.     def draw(self):
  63.         self.tk.canvas.move(self.id, self.x, self.y)
  64.     def left(self, event):
  65.         pos = self.tk.canvas.coords(self.id)
  66.         self.x = -2
  67.     def right(self, event):
  68.         pos = self.tk.canvas.coords(self.id)
  69.         self.x = 2
  70.            
  71. if __name__ == "__main__":
  72.     g = Game()
  73.     p = Paddle(g)
  74.     b = Ball(g, p)
  75.     g.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment