Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- from Tkinter import *
- from random import randint
- class Timer(object):
- def start(self):
- if hasattr(self, 'interval'):
- del self.interval
- self.start_time = time.time()
- def stop(self):
- if hasattr(self, 'start_time'):
- self.interval = time.time() - self.start_time
- del self.start_time
- def restart(self):
- self.stop()
- self.start()
- def getInterval(self):
- if hasattr(self, 'start_time'):
- self.interval = time.time() - self.start_time
- return self.interval
- class App:
- def __init__(self, title):
- self.window = Tk()
- self.window.title("Mdr")
- self.score = Label(self.window);
- self.canvas = Canvas(self.window, bg="black", width=720, height=720)
- self.player = Player()
- self.square = Square(self.canvas, 50)
- self.timer = Timer()
- self.points = 0
- self.timeToClick = 2
- self.score['text'] = "Cliquez sur le carre pour commencer!"
- self.score.pack(side=TOP)
- self.canvas.pack()
- self.bind("<Button-1>", self.updateGame)
- def getCanvas(self):
- return self.canvas
- def bind(self, button_name, func):
- self.canvas.bind(button_name, func)
- def addRectangle(self, x, y, size, fill='white'):
- print 'lol'
- def updateScore(self):
- self.points += 1
- self.score['text'] = str(round(self.points * 100 + self.timeToClick + self.timer.getInterval()))
- self.score['text'] += "\tVie " + str(self.player.getLives())
- def updateGame(self, event):
- self.player.getEvent(event)
- if self.player.isWithinSquare(self.square) == True:
- self.square.setNewEmplacement(self.getCanvas())
- self.updateScore()
- self.timeToClick -= 0.15
- self.timer.restart()
- else:
- self.player.lives -= 1
- def game(self):
- if self.timer.getInterval() > self.timeToClick:
- self.timer.restart()
- self.player.lives -= 1
- if self.player.isDead() == True:
- self.timer.stop()
- self.canvas.configure(bg='red')
- self.window.after(1, self.game)
- def run(self):
- self.timer.start()
- self.game()
- self.window.mainloop()
- class Player:
- def __init__(self):
- self.x = 0
- self.y = 0
- self.lives = 3
- def getEvent(self, event):
- self.x = event.x
- self.y = event.y
- def getLives(self):
- return self.lives
- def setCoordX(self, newX):
- self.x = newX
- def setCoordY(self, newY):
- self.y = newY
- def getCoordX(self):
- return self.x
- def getCoordY(self):
- return self.y
- def isDead(self):
- if self.lives >= 0:
- return False
- return True
- def isWithinSquare(self, square):
- if square.getCoordX() <= self.x and square.getCoordX() + square.getSize() > self.x and square.getCoordY() < self.y and square.getCoordY() + square.getSize() > self.y:
- return True
- return False
- class Square:
- def __init__(self, canvas, size, color='white'):
- self.size = size
- self.x = randint(0, 720 - size)
- self.y = randint(0, 650 - size)
- self.square = canvas.create_rectangle(self.x, self.y, self.x + size, self.y + size, fill=color)
- def setNewEmplacement(self, canvas):
- self.x = randint(50, 670)
- self.y = randint(50, 670)
- canvas.coords(self.square, self.x, self.y, self.x + self.size, self.y + self.size)
- def getSize(self):
- return self.size;
- def getCoordX(self):
- return self.x
- def getCoordY(self):
- return self.y
- #tout mettre dans app
- def main():
- app = App("Mdr")
- app.run()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement