Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import time
- import random
- class MoveableShape():
- def __init__(self):
- self.collider = Collider2D(self.canvas.coords(self.shape))
- def move(self, velocity):
- self.canvas.move(
- self.shape,
- velocity.x,
- velocity.y)
- self.collider.updateColliderVertices(self.canvas.coords(self.shape))
- def changeMoveDirection(self, direction):
- self.direction = direction
- class Ball(MoveableShape):
- def __init__(self, canvas, size, color, x, y):
- self.canvas = canvas
- self.shape = self.canvas.create_oval(x, y, x + size, y + size, fill=color)
- super().__init__()
- self.moveDirections = [
- Vector2(0.7, -0.7), # 45 degree
- Vector2(0.5, -0.87), # 60 degree
- Vector2(0, -1), # 90 degree
- Vector2(-0.5, -0.87), # 120 degree
- Vector2(-0.7, -0.7) # 135 degree
- ]
- self.setRandomMovmentVector()
- def move(self, speed, platform, sideColliders, upCollider, bricks, downCollider):
- self.checkCollisions(platform, sideColliders, upCollider, bricks, downCollider)
- super().move(self.direction * speed)
- def checkCollisions(self, platform, sideColliders, upCollider, bricks, downCollider):
- if (self.collider.isColliding(downCollider)):
- canvas.delete(self.shape)
- Restart()
- if (self.collider.isColliding(platform.collider)):
- self.setRandomMovmentVector()
- if (self.collider.isColliding(upCollider)):
- self.changeMoveDirection(self.direction * -1)
- if (any(map(self.collider.isColliding, sideColliders))):
- self.changeMoveDirection(
- Vector2(self.direction.x * -1, self.direction.y))
- for b in bricks:
- if (self.collider.isColliding(b.collider)):
- b.destroyShape()
- bricks.remove(b)
- self.changeMoveDirection(self.direction * -1)
- global score
- score.updateScore("Score: ", 10, Vector2(60, 490))
- break
- def setRandomMovmentVector(self):
- self.direction = random.choices(self.moveDirections)[0]
- class Platform(MoveableShape):
- def __init__(self, canvas, width, height, color, x, y):
- self.canvas = canvas
- self.shape = self.canvas.create_rectangle(x, y, x + width, y + height, fill=color)
- super().__init__()
- def moveLeft(self, speed):
- super().move(Vector2(1, 0) * speed)
- def moveRight(self, speed):
- super().move(Vector2(-1, 0) * speed)
- class Brick():
- def __init__(self, canvas, width, height, color, x, y):
- self.canvas = canvas
- self.shape = self.canvas.create_rectangle(x, y, x + width, y + height, fill=color)
- self.collider = Collider2D(self.canvas.coords(self.shape))
- def destroyShape(self):
- self.canvas.delete(self.shape)
- class Score():
- def __init__(self, canvas, color, position):
- self.canvas = canvas
- self.color = color
- self.scoreText = canvas.create_text(position.x, position.y, text="Score: 0", fill=color, font=("Console", 16))
- self.scorePoints = 0
- def updateScore(self, text, addPoints, position):
- self.scorePoints += addPoints
- canvas.delete(self.scoreText)
- self.scoreText = canvas.create_text(position.x, position.y, text=text + str(self.scorePoints),
- fill=self.color, font=("Console", 16))
- class Collider2D():
- def __init__(self, coords):
- self.updateColliderVertices(coords)
- def updateColliderVertices(self, coords):
- self.upLeft = Vector2(coords[0], coords[1])
- self.downRight = Vector2(coords[2], coords[3])
- def isColliding(self, secondCollider):
- return (self.downRight.x > secondCollider.upLeft.x and
- self.upLeft.x < secondCollider.downRight.x and
- self.downRight.y > secondCollider.upLeft.y and
- self.upLeft.y < secondCollider.downRight.y)
- class Vector2():
- def __init__(self, x, y):
- self.x = x
- self.y = y
- def __mul__(self, other):
- return Vector2(self.x * other, self.y * other)
- def createCanvas(x, y):
- window = Tk()
- canvas = Canvas(window, width=x, height=y)
- canvas.pack()
- return canvas
- def createBallAtCanvasCenter(size, color):
- x = (canvas.winfo_reqwidth() - size) / 2
- y = (canvas.winfo_reqheight() - size) / 2
- return Ball(canvas, size, color, x, y)
- def createPlatform(width, height, color, y, speed):
- x = (canvas.winfo_reqwidth() - width) / 2
- platform = Platform(canvas, width, height, color, x, y)
- canvas.bind_all('<KeyPress-Right>',
- lambda _: platform.moveLeft(speed))
- canvas.bind_all('<KeyPress-Left>',
- lambda _: platform.moveRight(speed))
- return platform
- def createSideColliders(Collider2D, canvasWidth, canvasHeight):
- leftCollider = Collider2D([-10, 0, 0, canvasHeight])
- rightCollider = Collider2D([canvasWidth, 0, canvasWidth + 10, canvasHeight])
- return [leftCollider, rightCollider]
- def createLevel(rows, columns):
- bricks = []
- paddingX = (canvas.winfo_reqwidth() - columns * brickWidth) / 2
- for i in range(rows):
- for j in range(columns):
- bricks.append(
- Brick(canvas, brickWidth, brickHeight,
- bricksColors[i % len(bricksColors)], paddingX + brickWidth * j, brickHeight/2 + brickHeight * i))
- return bricks
- def StartGame():
- canvas.delete("all")
- ball = createBallAtCanvasCenter(ballSize, ballColor)
- platform = createPlatform(platformWidth, platformHeight, platformColor, platformY, platformSpeed)
- global score
- score = Score(canvas, scoreColor, scoreDefaultPosition)
- bricks = createLevel(rows, columns)
- while True:
- ball.move(ballSpeed, platform, sideColliders,
- upCollider, bricks, downCollider)
- canvas.update()
- time.sleep(0.01)
- def Restart():
- global score
- score.updateScore("Game Over! Score: ", 0, Vector2(120, 490))
- canvas.update()
- time.sleep(3)
- StartGame()
- # region GameSettings
- canvasWidth = 500
- canvasHeight = 500
- ballSize = 30
- ballSpeed = 3
- ballColor = "#AD1457"
- platformWidth = 100
- platformHeight = 20
- platformSpeed = 10
- platformColor = "#AD1457"
- platformY = 450
- brickWidth = 60
- brickHeight = 20
- bricksColors = ["#AD1457", "#F06292", "#F48FB1"]
- rows = 3
- columns = int(canvasWidth / brickWidth)
- scoreColor = "#F06292"
- scoreDefaultPosition = Vector2(60, 490)
- # endregion
- canvas = createCanvas(canvasWidth, canvasHeight)
- score = NONE
- sideColliders = createSideColliders(Collider2D, canvasWidth, canvasHeight)
- upCollider = Collider2D([0, -10, canvasWidth, 0])
- downCollider = Collider2D([0, canvasHeight, canvasWidth, canvasHeight + 10])
- StartGame()
Advertisement
Add Comment
Please, Sign In to add comment