Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import time
- from random import randrange as rnd, random, choice
- from math import atan
- #~ Бонусы:
- #~ 1. быстрый мяч +
- #~ 2. медленный мяч +
- #~ 3. случайный отскок
- #~ 4. короткая платформа +
- #~ 5. длинная платформа +
- #~ 6. три мяча +
- #~ 7. три черных мяча (нельзя терять ни один)
- #~ 8. пол на время +
- #~ 9. пол - страховка (на удар)
- #~ 10. огненный мяч (сбивает все подряд, бонусов нет) +
- #~ 11. сильный мяч (сбивает сбиваемые, отскакивает) +
- #~ 12. выбиватель бонусов (выбивает чаще)
- #~ 13. жизнь +
- #~ 14. уничтожение всех блоков
- #~ 15. липучка (на время)
- #~ 16. целеуказатель (линия)
- #~ 17. мяч ловит бонусы
- #~ 18. пушка
- #~ 19. неизвестный бонус
- root = Tk()
- fr = Frame(root)
- root.geometry('850x600')
- canv = Canvas(root, bg = 'white')
- canv.pack(fill=BOTH,expand=1)
- colors = ['green','yellow','orange', 'red']
- bonuses = ('long','short','fire','slow','fast','balls','live','floor','energy_ball','gun')
- class Bullet():
- def __init__(self,x,y):
- self.x = x
- self.y = y
- self.r = r = 3
- self.vy = -15
- self.vx = 0
- self.id = canv.create_oval(x-r,y-r,x+r,y+r,fill='black')
- self.kill = False
- self.move()
- def move(self):
- if mode == 'game' and self.y > 0 and not self.kill:
- self.y += self.vy
- for block in blocks:
- if block.x < self.x < block.x + block.w and block.y < self.y < block.y+block.h:
- block.kill()
- self.kill = True
- break
- canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
- root.after(30,self.move)
- else:
- canv.delete(self.id)
- class Bonus():
- def __init__(self,x,y):
- self.x = x + 30
- self.y = y
- self.vy = 3
- self.vx = 0
- self.bonus = choice(bonuses)
- self.id = canv.create_text(self.x,self.y,text = self.bonus)
- self.move()
- def move(self):
- self.x += self.vx
- self.y += self.vy
- if self.y > 600:
- canv.delete(self.id)
- else:
- if p.x < self.x < p.x+p.w and p.y < self.y < p.y + p.h:
- canv.delete(self.id)
- p.bonus(self.bonus)
- else:
- if mode == 'game':
- root.after(30,self.move)
- canv.coords(self.id,self.x,self.y)
- else:
- canv.delete(self.id)
- class Block():
- def __init__(self,x,y,k):
- self.x = x
- self.y = y
- self.h = 20
- self.w = 60
- self.k = k
- self.id = canv.create_rectangle(self.x,self.y,self.x+self.w,self.y+self.h,fill=colors[self.k],width=2)
- def kick(self):
- self.k -= 1
- if p.isFire or p.isEnergy_ball:
- self.k = -1
- if self.k < 0:
- self.kill()
- def check(self):
- for ball in balls:
- if not ball.kick:
- if self.x-ball.r < ball.x < self.x + self.w + ball.r and self.y-ball.r < ball.y < self.y + self.h+ball.r:
- if not p.isFire:
- prev_ball_x = ball.x -ball.vx
- prev_ball_y = ball.y -ball.vy
- if abs(ball.vy) > 1 and prev_ball_y < self.y or prev_ball_y > self.y+self.h:
- ball.vy *= -1
- ball.kick = True
- if prev_ball_x < self.x or prev_ball_x > self.x+self.w:
- ball.kick = True
- ball.vx *= -1
- if abs(ball.vx) < 1:
- ball.vx *= 3
- self.kick()
- if self.k >= 0:
- canv.itemconfig(self.id,fill=colors[self.k])
- def kill(self):
- if not p.isFire:
- Bonus(self.x,self.y)
- if self in blocks:
- blocks.remove(self)
- canv.delete(self.id)
- if not blocks:
- next_level()
- class Ball():
- def __init__(self,x=400,y=500,vx=0,vy=6):
- self.x = x
- self.y = y
- self.r = 8
- self.kick = False
- self.color = 'orange'
- self.id = canv.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r,fill=self.color,tag='balls')
- self.vy = vy
- self.vx = vx
- def move(self):
- if mode == 'init':
- self.x = p.x+p.w/2
- self.y = p.y-self.r
- else:
- self.kick = False
- self.x += self.vx
- self.y += self.vy
- if self.y < 50:
- self.y = 50
- self.vy *= -1
- if self.x < 50:
- self.x = 50
- self.vx *= -1
- if self.x > 750:
- self.x = 750
- self.vx *= -1
- if self.y > 580 and p.isFloor > 0:
- self.vy *= -1
- if self.y > 600:
- self.kill()
- canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
- canv.itemconfig(self.id,fill=self.color)
- def kill(self):
- canv.delete(self.id)
- balls.remove(self)
- if not balls:
- p.lives -= 1
- init()
- class Platform():
- def __init__(self):
- self.x = 300
- self.y = 550
- self.ax = 0
- self.w = 60
- self.h = 20
- self.id = canv.create_rectangle(self.x,self.y,self.x+self.w,self.y+self.h,fill='green',width=0)
- self.vx = 0
- self.vy = 0
- self.lives = 2
- self.line = False
- self.isFloor = 0
- self.isFire = 0
- self.isEnergy_ball = 0
- self.isGun = 300
- self.gun_cool_time = 10
- self.fire_id = canv.create_text(790,400,text = self.isFire)
- self.enery_ball_id = canv.create_text(790,425,text = self.isEnergy_ball)
- self.floor_id = canv.create_text(790,450,text = self.isFloor)
- self.gun_id = canv.create_text(790,475,text = self.isGun)
- if self.x < 50:
- self.ax = 0
- self.vx = 0
- if self.x > 750:
- self.ax = 0
- self.vx = 0
- def gun(self):
- self.isGun += 300
- def energy_ball(self):
- self.isEnergy_ball += 300
- for ball in balls:
- ball.color = 'blue'
- self.isFire = 0
- def unenergy_ball(self):
- for ball in balls:
- ball.color = 'orange'
- def fire(self):
- self.isFire += 500
- for ball in balls:
- ball.color = 'red'
- self.isEnergy_ball = 0
- def unfire(self):
- for ball in balls:
- ball.color = 'orange'
- def floor(self):
- self.isFloor += 300
- canv.create_line(50,580,750,580,width=3,tag='floor')
- def unfloor(self):
- canv.delete('floor')
- def bonus(self,bonus):
- if bonus == 'long':
- if self.w < 140:
- self.w += 20
- self.x -= 10
- elif bonus == 'short':
- if self.w > 40:
- self.w -= 20
- self.x += 10
- elif bonus == 'balls':
- balls.append(Ball(p.x+p.w/2,p.y-10,random()*2-1,-5))
- balls.append(Ball(p.x+p.w/2,p.y-10,random()*2-1,-5))
- balls.append(Ball(p.x+p.w/2,p.y-10,random()*2-1,-5))
- if self.isFire > 0:
- for ball in balls:
- ball.color = 'red'
- elif bonus == 'three_ball':
- balls.append(Ball(p.x+p.w/2,p.y-10,random()*2-1,-3))
- balls.append(Ball(p.x+p.w/2,p.y-10,random()*2-1,-3))
- balls.append(Ball(p.x+p.w/2,p.y-10,random()*2-1,-3))
- self.three_ball += 300
- for ball in balls:
- ball.color = 'black'
- elif bonus == 'fast':
- for ball in balls:
- ball.vx *= 1.2
- ball.vy *= 1.2
- elif bonus == 'slow':
- for ball in balls:
- ball.vx *= 0.8
- ball.vy *= 0.8
- elif bonus == 'fire':
- self.fire()
- elif bonus == 'energy_ball':
- self.energy_ball()
- elif bonus == 'live':
- p.lives += 1
- show_lives()
- elif bonus == 'line':
- p.line = True
- elif bonus == 'floor':
- p.floor()
- elif bonus == 'gun':
- print(1)
- p.gun()
- def move(self):
- self.vx += self.ax
- self.x += self.vx
- self.vx *= 0.9
- if self.x+self.w > 750:
- self.x = 750-self.w
- if self.x < 50:
- self.x = 50
- for ball in balls:
- if p.x <= ball.x+ball.r and ball.x-ball.r < p.x +p.w and p.y <= ball.y+ball.r and ball.y-ball.r <= p.y+p.h:
- ball.vy *= -1
- ball.vx = (ball.x - (p.x+p.w/2))/7
- ball.y = p.y - ball.r
- if self.isFloor > 0:
- self.isFloor -= 1
- if self.isFloor <= 0:
- self.unfloor()
- if self.isFire > 0:
- self.isFire -= 1
- if self.isFire <= 0:
- self.unfire()
- if self.isEnergy_ball > 0:
- self.isEnergy_ball -= 1
- if self.isEnergy_ball <= 0:
- self.unenergy_ball()
- if self.isGun > 0:
- self.isGun -= 1
- self.gun_cool_time -= 1
- canv.coords(self.id,self.x,self.y,self.x+self.w,self.y+self.h)
- canv.itemconfig(self.fire_id,text = self.isFire)
- canv.itemconfig(self.floor_id,text = self.isFloor)
- canv.itemconfig(self.enery_ball_id,text = self.isEnergy_ball)
- canv.itemconfig(self.gun_id,text = self.isGun)
- def new_game(event=0):
- global balls, p, keys, level
- level = 1
- canv.create_rectangle(0,0,900,600,fill='gray')
- canv.create_rectangle(50,50,750,700,fill='white')
- load_level()
- root.bind('<Key>',keyDown)
- root.bind('<KeyRelease>',keyUp)
- keys = set()
- balls = []
- p = Platform()
- init()
- def load_level():
- global blocks, level
- blocks = []
- try:
- f = open('level' + str(level) + '.txt','r')
- except:
- level = 1
- f = open('level' + str(level) + '.txt','r')
- x = 100
- y = 100
- for line in f.readlines():
- for hp in line.strip():
- if hp != '-':
- blocks.append(Block(x,y,int(hp)))
- x += 65
- y += 25
- x = 100
- h = 40
- f.close()
- def init():
- global mode, balls
- if p.lives < 0:
- game_over()
- p.x = 400 - p.w/2
- p.vx = 0
- p.ax = 0
- mode = 'init'
- canv.delete('balls')
- balls = [Ball()]
- # standart settings, remove bonuses...
- p.w = 60
- p.isFire = 0
- p.isFloor = 0
- p.isEnergy_ball = 0
- show_lives()
- def show_lives():
- canv.delete('lives')
- y = 550
- for i in range(p.lives):
- canv.create_oval(790,y,800,y+10,fill='blue', tag = 'lives')
- y -= 18
- def next_level():
- global level
- level += 1
- load_level()
- init()
- def game_over():
- root.unbind('<Key>')
- root.unbind('<KeyRelease>')
- canv.create_text(400,300,text='Game over', font = 'Arial 100')
- canv.bind('<1>',new_game)
- def keyDown(event):
- global mode
- if mode == 'init' and event.keycode == 65:
- mode = 'game'
- balls[-1].vy = -6
- elif event.keycode == 65 and p.isGun > 0 and p.gun_cool_time < 0:
- Bullet(p.x+p.w/2,p.y)
- p.gun_cool_time = 30
- else:
- keys.add(event.keycode)
- def keyUp(event):
- if event.keycode in keys:
- keys.remove(event.keycode)
- def move(event):
- p.x = event.x
- p.move()
- new_game()
- while 1:
- p.ax = 0
- if 113 in keys:
- p.ax = -2
- if 114 in keys:
- p.ax = 2
- for ball in balls:
- ball.move()
- p.move()
- for block in blocks:
- block.check()
- canv.update()
- time.sleep(0.03)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement