Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from random import randrange as rnd, choice
- import time
- root = Tk()
- root.geometry('800x600')
- canv = Canvas(root,bg='white')
- canv.pack(fill=BOTH,expand=1)
- colors = ['violet', 'brown', 'red', 'orange', 'yellow', 'green','blue']
- #1
- class Ball():
- def __init__(self):
- self.x = 400
- self.y = 300
- self.r = 6
- self.vx = rnd(-5,6)
- self.vy = 6
- self.k =0
- self.id = canv.create_oval(self.x - self.r, self.y - self.r, self.x + self.r, self.y + self.r, fill = 'red', width = 0)
- def move(self):
- self.x += self.vx
- self.y += self.vy
- if self.y > 650:
- self.vy = -self.vy
- if self.x > 750:
- self.vx =- self.vx
- if self.y < 50:
- self.vy = -self.vy
- if self.x < 50:
- self.vx =- self.vx
- canv.coords(self.id,self.x - self.r, self.y - self.r, self.x + self.r, self.y + self.r)
- ball = Ball()
- class Platform():
- def __init__(self,x,y):
- self.w = 100
- self.x = x - self.w/2
- self.y = y
- self.h = 20
- self.vx = 0
- self.ax = 0
- self.id = canv.create_rectangle(self.x,self.y,self.x + self.w,self.y + self.h, fill = 'green')
- def move(self):
- if self.x > 750 - self.w:
- self.vx = 0
- self.ax = 0
- self.x = 750 - self.w
- if self.x < 50:
- self.vx = 0
- self.ax = 0
- self.x = 50
- self.vx *= 0.8
- self.vx += self.ax
- self.x += self.vx
- if self.x < ball.x < self.x + self.w and self.y < ball.y < self.y + self.h:
- ball.vy = -ball.vy
- ball.vx = (ball.x - (self.x+self.w/2))/5
- canv.coords(self.id,self.x,self.y,self.x + self.w,self.y + self.h)
- plat1 = Platform(400,550)
- def keyDown(event):
- print(event.keycode)
- keys.add(event.keycode)
- def keyUp(event):
- keys.remove(event.keycode)
- keys = set()
- root.bind('<Key>',keyDown)
- root.bind('<KeyRelease>',keyUp)
- colors = ['yellow','orange','red','green','blue']
- class Block():
- def __init__(self,x,y,hp=0):
- self.w = 60
- self.x = x
- self.y = y
- self.h = 20
- self.vx = 0
- self.ax = 0
- self.hp = hp
- self.id = canv.create_rectangle(self.x,self.y,self.x + self.w,self.y + self.h, fill = colors[self.hp])
- def check(self):
- if self.x < ball.x < self.x + self.w and self.y < ball.y < self.y+self.h:
- x = ball.x - ball.vx
- y = ball.y - ball.vy
- if x < self.x or x > self.x+self.w:
- ball.vx = - ball.vx
- if y < self.y or y > self.y + self.h:
- ball.vy = -ball.vy
- self.hp -= 1
- canv.itemconfig(self.id, fill = colors[self.hp])
- if self.hp < 0:
- self.kill()
- return True
- return False
- def kill(self):
- canv.delete(self.id)
- blocks.remove(self)
- def change_colors():
- for bl in blocks:
- canv.itemconfig(bl.id,fill=choice(colors))
- root.after(1000,change_colors)
- blocks = []
- #change_colors()
- f = open('level.txt','r')
- y = 50
- for line in f.readlines():
- x = 50
- for s in line.strip():
- if s != '-':
- blocks.append(Block(x,y,int(s)))
- x += 65
- y += 35
- f.close()
- while 1:
- ball.move()
- for block in blocks:
- if block.check():
- break
- plat1.ax = 0
- if 113 in keys:
- plat1.ax = -3
- if 114 in keys:
- plat1.ax = +3
- plat1.move()
- canv.update()
- time.sleep(0.03)
- mainloop()
- --- level.txt ----
- -
- -
- ---000000---
- --00033000--
- ----0000----
- -----00-----
Advertisement
Add Comment
Please, Sign In to add comment