Advertisement
Le_BuG63

lol

Feb 20th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.91 KB | None | 0 0
  1. import time
  2.  
  3. from Tkinter import *
  4. from random import randint
  5.  
  6. class Timer(object):
  7.     def start(self):
  8.         if hasattr(self, 'interval'):
  9.             del self.interval
  10.         self.start_time = time.time()
  11.  
  12.     def stop(self):
  13.         if hasattr(self, 'start_time'):
  14.             self.interval = time.time() - self.start_time
  15.             del self.start_time
  16.  
  17.     def restart(self):
  18.         self.stop()
  19.         self.start()
  20.  
  21.     def getInterval(self):
  22.         if hasattr(self, 'start_time'):
  23.             self.interval = time.time() - self.start_time
  24.             return self.interval
  25.  
  26. class App:
  27.     def __init__(self, title):
  28.         self.window = Tk()
  29.         self.window.title("Mdr")
  30.  
  31.         self.score = Label(self.window);
  32.         self.canvas = Canvas(self.window, bg="black", width=720, height=720)
  33.  
  34.         self.player = Player()
  35.         self.square = Square(self.canvas, 50)
  36.         self.timer = Timer()
  37.  
  38.         self.points = 0
  39.         self.timeToClick = 2
  40.  
  41.         self.score['text'] = "Cliquez sur le carre pour commencer!"
  42.         self.score.pack(side=TOP)
  43.  
  44.         self.canvas.pack()
  45.  
  46.         self.bind("<Button-1>", self.updateGame)
  47.     def getCanvas(self):
  48.         return self.canvas
  49.  
  50.     def bind(self, button_name, func):
  51.         self.canvas.bind(button_name, func)
  52.  
  53.     def addRectangle(self, x, y, size, fill='white'):
  54.         print 'lol'
  55.  
  56.     def updateScore(self):
  57.         self.points += 1
  58.         self.score['text'] = str(round(self.points * 100 + self.timeToClick + self.timer.getInterval()))
  59.         self.score['text'] += "\tVie " + str(self.player.getLives())
  60.  
  61.     def updateGame(self, event):
  62.         self.player.getEvent(event)
  63.  
  64.         if self.player.isWithinSquare(self.square) == True:
  65.             self.square.setNewEmplacement(self.getCanvas())
  66.             self.updateScore()
  67.             self.timeToClick -= 0.15
  68.             self.timer.restart()
  69.         else:
  70.             self.player.lives -= 1
  71.  
  72.     def game(self):
  73.         if self.timer.getInterval() > self.timeToClick:
  74.             self.timer.restart()
  75.             self.player.lives -= 1
  76.  
  77.         if self.player.isDead() == True:
  78.             self.timer.stop()
  79.             self.canvas.configure(bg='red')
  80.  
  81.         self.window.after(1, self.game)
  82.  
  83.     def run(self):
  84.         self.timer.start()
  85.         self.game()
  86.         self.window.mainloop()
  87.  
  88. class Player:
  89.     def __init__(self):
  90.         self.x = 0
  91.         self.y = 0
  92.         self.lives = 3
  93.  
  94.     def getEvent(self, event):
  95.         self.x = event.x
  96.         self.y = event.y
  97.  
  98.     def getLives(self):
  99.         return self.lives
  100.  
  101.     def setCoordX(self, newX):
  102.         self.x = newX
  103.  
  104.     def setCoordY(self, newY):
  105.         self.y = newY
  106.  
  107.     def getCoordX(self):
  108.         return self.x
  109.  
  110.     def getCoordY(self):
  111.         return self.y
  112.  
  113.     def isDead(self):
  114.         if self.lives >= 0:
  115.             return False
  116.         return True
  117.  
  118.     def isWithinSquare(self, square):
  119.         if square.getCoordX() <= self.x and square.getCoordX() + square.getSize() > self.x and square.getCoordY() < self.y and square.getCoordY() + square.getSize() > self.y:
  120.             return True
  121.         return False
  122.  
  123. class Square:
  124.     def __init__(self, canvas, size, color='white'):
  125.         self.size = size
  126.  
  127.         self.x = randint(0, 720 - size)
  128.         self.y = randint(0, 650 - size)
  129.  
  130.         self.square = canvas.create_rectangle(self.x, self.y, self.x + size, self.y + size, fill=color)
  131.  
  132.     def setNewEmplacement(self, canvas):
  133.         self.x = randint(50, 670)
  134.         self.y = randint(50, 670)
  135.  
  136.         canvas.coords(self.square, self.x, self.y, self.x + self.size, self.y + self.size)
  137.  
  138.     def getSize(self):
  139.         return self.size;
  140.  
  141.     def getCoordX(self):
  142.         return self.x
  143.  
  144.     def getCoordY(self):
  145.         return self.y
  146.  
  147. #tout mettre dans app
  148. def main():
  149.     app = App("Mdr")
  150.     app.run()
  151.  
  152. if __name__ == '__main__':
  153.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement