Griff1th

PyArkanoid

Dec 30th, 2022 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.93 KB | Gaming | 0 0
  1. from tkinter import *
  2. import time
  3. import random
  4.  
  5.  
  6. class MoveableShape():
  7.     def __init__(self):
  8.         self.collider = Collider2D(self.canvas.coords(self.shape))
  9.  
  10.     def move(self, velocity):
  11.         self.canvas.move(
  12.             self.shape,
  13.             velocity.x,
  14.             velocity.y)
  15.  
  16.         self.collider.updateColliderVertices(self.canvas.coords(self.shape))
  17.  
  18.     def changeMoveDirection(self, direction):
  19.         self.direction = direction
  20.  
  21.  
  22. class Ball(MoveableShape):
  23.     def __init__(self, canvas, size, color, x, y):
  24.         self.canvas = canvas
  25.         self.shape = self.canvas.create_oval(x, y, x + size, y + size, fill=color)
  26.         super().__init__()
  27.  
  28.         self.moveDirections = [
  29.             Vector2(0.7, -0.7),    # 45 degree
  30.             Vector2(0.5, -0.87),  # 60 degree
  31.             Vector2(0, -1),        # 90 degree
  32.             Vector2(-0.5, -0.87),  # 120 degree
  33.             Vector2(-0.7, -0.7)    # 135 degree
  34.         ]
  35.         self.setRandomMovmentVector()
  36.  
  37.     def move(self, speed, platform, sideColliders, upCollider, bricks, downCollider):
  38.         self.checkCollisions(platform, sideColliders, upCollider, bricks, downCollider)
  39.         super().move(self.direction * speed)
  40.  
  41.     def checkCollisions(self, platform, sideColliders, upCollider, bricks, downCollider):
  42.         if (self.collider.isColliding(downCollider)):
  43.             canvas.delete(self.shape)
  44.             Restart()
  45.         if (self.collider.isColliding(platform.collider)):
  46.             self.setRandomMovmentVector()
  47.         if (self.collider.isColliding(upCollider)):
  48.             self.changeMoveDirection(self.direction * -1)
  49.         if (any(map(self.collider.isColliding, sideColliders))):
  50.             self.changeMoveDirection(
  51.                 Vector2(self.direction.x * -1, self.direction.y))
  52.         for b in bricks:
  53.             if (self.collider.isColliding(b.collider)):
  54.                 b.destroyShape()
  55.                 bricks.remove(b)
  56.                 self.changeMoveDirection(self.direction * -1)
  57.                 global score
  58.                 score.updateScore("Score: ", 10, Vector2(60, 490))
  59.                 break
  60.  
  61.     def setRandomMovmentVector(self):
  62.         self.direction = random.choices(self.moveDirections)[0]
  63.  
  64.  
  65. class Platform(MoveableShape):
  66.     def __init__(self, canvas, width, height, color, x, y):
  67.         self.canvas = canvas
  68.         self.shape = self.canvas.create_rectangle(x, y, x + width, y + height, fill=color)
  69.         super().__init__()
  70.  
  71.     def moveLeft(self, speed):
  72.         super().move(Vector2(1, 0) * speed)
  73.  
  74.     def moveRight(self, speed):
  75.         super().move(Vector2(-1, 0) * speed)
  76.  
  77.  
  78. class Brick():
  79.     def __init__(self, canvas, width, height, color, x, y):
  80.         self.canvas = canvas
  81.         self.shape = self.canvas.create_rectangle(x, y, x + width, y + height, fill=color)
  82.         self.collider = Collider2D(self.canvas.coords(self.shape))
  83.  
  84.     def destroyShape(self):
  85.         self.canvas.delete(self.shape)
  86.  
  87.  
  88. class Score():
  89.     def __init__(self, canvas, color, position):
  90.         self.canvas = canvas
  91.         self.color = color
  92.         self.scoreText = canvas.create_text(position.x, position.y, text="Score: 0", fill=color, font=("Console", 16))
  93.         self.scorePoints = 0
  94.  
  95.     def updateScore(self, text, addPoints, position):
  96.         self.scorePoints += addPoints
  97.         canvas.delete(self.scoreText)
  98.         self.scoreText = canvas.create_text(position.x, position.y, text=text + str(self.scorePoints),
  99.         fill=self.color, font=("Console", 16))
  100.  
  101.  
  102. class Collider2D():
  103.     def __init__(self, coords):
  104.         self.updateColliderVertices(coords)
  105.  
  106.     def updateColliderVertices(self, coords):
  107.         self.upLeft = Vector2(coords[0], coords[1])
  108.         self.downRight = Vector2(coords[2], coords[3])
  109.  
  110.     def isColliding(self, secondCollider):
  111.         return (self.downRight.x > secondCollider.upLeft.x and
  112.                 self.upLeft.x < secondCollider.downRight.x and
  113.                 self.downRight.y > secondCollider.upLeft.y and
  114.                 self.upLeft.y < secondCollider.downRight.y)
  115.  
  116.  
  117. class Vector2():
  118.     def __init__(self, x, y):
  119.         self.x = x
  120.         self.y = y
  121.  
  122.     def __mul__(self, other):
  123.         return Vector2(self.x * other, self.y * other)
  124.  
  125.  
  126. def createCanvas(x, y):
  127.     window = Tk()
  128.     canvas = Canvas(window, width=x, height=y)
  129.     canvas.pack()
  130.     return canvas
  131.  
  132.  
  133. def createBallAtCanvasCenter(size, color):
  134.     x = (canvas.winfo_reqwidth() - size) / 2
  135.     y = (canvas.winfo_reqheight() - size) / 2
  136.     return Ball(canvas, size, color, x, y)
  137.  
  138.  
  139. def createPlatform(width, height, color, y, speed):
  140.     x = (canvas.winfo_reqwidth() - width) / 2
  141.     platform = Platform(canvas, width, height, color, x, y)
  142.     canvas.bind_all('<KeyPress-Right>',
  143.                     lambda _: platform.moveLeft(speed))
  144.     canvas.bind_all('<KeyPress-Left>',
  145.                     lambda _: platform.moveRight(speed))
  146.     return platform
  147.  
  148.  
  149. def createSideColliders(Collider2D, canvasWidth, canvasHeight):
  150.     leftCollider = Collider2D([-10, 0, 0, canvasHeight])
  151.     rightCollider = Collider2D([canvasWidth, 0, canvasWidth + 10, canvasHeight])
  152.     return [leftCollider, rightCollider]
  153.  
  154.  
  155. def createLevel(rows, columns):
  156.     bricks = []
  157.     paddingX = (canvas.winfo_reqwidth() - columns * brickWidth) / 2
  158.     for i in range(rows):
  159.         for j in range(columns):
  160.             bricks.append(
  161.                 Brick(canvas, brickWidth, brickHeight,
  162.                       bricksColors[i % len(bricksColors)], paddingX + brickWidth * j, brickHeight/2 + brickHeight * i))
  163.  
  164.     return bricks
  165.  
  166.  
  167. def StartGame():
  168.     canvas.delete("all")
  169.     ball = createBallAtCanvasCenter(ballSize, ballColor)
  170.     platform = createPlatform(platformWidth, platformHeight, platformColor, platformY, platformSpeed)
  171.     global score
  172.     score = Score(canvas, scoreColor, scoreDefaultPosition)
  173.     bricks = createLevel(rows, columns)
  174.     while True:
  175.         ball.move(ballSpeed,  platform, sideColliders,
  176.                   upCollider, bricks, downCollider)
  177.         canvas.update()
  178.         time.sleep(0.01)
  179.  
  180.  
  181. def Restart():
  182.     global score
  183.     score.updateScore("Game Over! Score: ", 0, Vector2(120, 490))
  184.     canvas.update()
  185.     time.sleep(3)
  186.     StartGame()
  187.  
  188.  
  189. # region GameSettings
  190. canvasWidth = 500
  191. canvasHeight = 500
  192.  
  193. ballSize = 30
  194. ballSpeed = 3
  195. ballColor = "#AD1457"
  196.  
  197. platformWidth = 100
  198. platformHeight = 20
  199. platformSpeed = 10
  200. platformColor = "#AD1457"
  201. platformY = 450
  202.  
  203. brickWidth = 60
  204. brickHeight = 20
  205. bricksColors = ["#AD1457", "#F06292", "#F48FB1"]
  206. rows = 3
  207. columns = int(canvasWidth / brickWidth)
  208.  
  209. scoreColor = "#F06292"
  210. scoreDefaultPosition = Vector2(60, 490)
  211. # endregion
  212.  
  213.  
  214. canvas = createCanvas(canvasWidth, canvasHeight)
  215. score = NONE
  216. sideColliders = createSideColliders(Collider2D, canvasWidth, canvasHeight)
  217. upCollider = Collider2D([0, -10, canvasWidth, 0])
  218. downCollider = Collider2D([0, canvasHeight, canvasWidth, canvasHeight + 10])
  219. StartGame()
  220.  
Advertisement
Add Comment
Please, Sign In to add comment