Nov-18-2017, 02:53 AM
Okay figure out keypress work smoothly. Enjoy.
import tkinter as tk from random import randint, choice, shuffle WIDTH = 1000 HEIGHT = 700 MID_X = WIDTH / 2 MID_Y = HEIGHT / 2 class FallingBlock: CHOICE = [0,0,0,0,2,3] + [1 for n in range(170)] def __init__(self, canvas, choice_id): self.value = randint(5, 10) x = randint(self.value + 1, WIDTH - self.value) self.color_id = choice_id color = ['yellow', 'yellow', 'green', 'orange'][self.color_id] self.id = canvas.create_rectangle(x - self.value, -self.value, x + self.value, self.value, outline=color) if self.color_id == 3: self.speed = 8 self.value *= 5 elif self.color_id == 2: self.speed = 4 self.value += 2 else: self.speed = randint(1, 2) def move(self, canvas): canvas.move(self.id, 0, self.speed) class Game: def __init__(self): self.window = tk.Tk() self.canvas = tk.Canvas(self.window, width=WIDTH, height=HEIGHT, bg='black') self.canvas.pack() self.window.bind_all('<KeyPress>', self.keys_press) self.window.bind_all('<KeyRelease>', self.keys_release) self.speed = 10 self.loop_id = None self.time_loop_id = None self.new_game() def new_game(self): shuffle(FallingBlock.CHOICE) self.falling_blocks = {} self.keys = {} self.score = 0 self.bonus = 0 self.time = 31 self.canvas.delete('all') self.canvas.create_text(50, 30, text='TIME', fill='white' ) self.canvas.create_text(150, 30, text='SCORE', fill='white' ) self.time_id = self.canvas.create_text(50, 50, fill='white' ) self.score_id = self.canvas.create_text(150, 50, fill='white', text='0') self.block_id = self.canvas.create_rectangle(0, 0, 40, 40, fill='red') self.canvas.move(self.block_id, MID_X, MID_Y) # stop timers if self.time_loop_id: self.window.after_cancel(self.time_loop_id) if self.loop_id: self.window.after_cancel(self.loop_id) # start time loops self.loop_id = None self.time_loop_id = None self.loop() self.time_loop() def keys_press(self, event): if event.keysym in ['Up', 'Down', 'Left', 'Right']: self.keys[event.keysym] = True elif event.keysym == 'Escape': self.window.destroy() elif event.keysym == 'n': self.new_game() def keys_release(self, event): if event.keysym in ['Up', 'Down', 'Left', 'Right']: self.keys[event.keysym] = False def collide(self, box): x, y, w, h = self.canvas.bbox(self.block_id) return ( (((x > box[0]) and (x < box[2])) or ((box[0] > x) and (box[0] < w))) and (((y > box[1]) and (y < box[3])) or ((box[1] > y) and (box[1] < h)))) def loop(self): if self.time < 0: self.canvas.create_text(MID_X, MID_Y, text='GAME OVER', fill='white', font=('Helvetica',30)) self.canvas.create_text(MID_X, MID_Y + 30, text='Score: '+ str(self.score), fill='white') # return for it can't assign another cycle return # Moving Player if self.keys.get('Left', False): self.canvas.move(self.block_id, -self.speed, 0) if self.keys.get('Right', False): self.canvas.move(self.block_id, self.speed, 0) if self.keys.get('Up', False): self.canvas.move(self.block_id, 0, -self.speed) if self.keys.get('Down', False): self.canvas.move(self.block_id, 0, self.speed) falling_id = choice(FallingBlock.CHOICE) if falling_id != 1 or len(self.falling_blocks) < 4: fall_block = FallingBlock(self.canvas, falling_id) self.falling_blocks[fall_block.id] = fall_block blocks_remove = [] for item_id, block in self.falling_blocks.items(): box = self.canvas.bbox(item_id) # move block block.move(self.canvas) # object fell offscreen if box[1] > HEIGHT + 1: blocks_remove.append(item_id) # collision else: if self.collide(box): if block.color_id == 2: self.time += 2 blocks_remove.append(item_id) self.score += block.value self.canvas.itemconfig(self.score_id, text=str(self.score)) for key in blocks_remove: self.canvas.delete(key) del self.falling_blocks[key] # roughly 30 frames per second self.loop_id = self.window.after(int(1000/30), self.loop) def time_loop(self): self.time -= 1 if self.time < 0: return self.canvas.itemconfig(self.time_id, text=str(self.time)) self.time_loop_id = self.window.after(1000, self.time_loop) def main(): game = Game() game.window.mainloop() main()
99 percent of computer problems exists between chair and keyboard.