├── requirements.txt ├── 1.jpg ├── screenshots └── screen.jpg ├── README.md ├── LICENSE └── main.py /requirements.txt: -------------------------------------------------------------------------------- 1 | pygame 2 | -------------------------------------------------------------------------------- /1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StanislavPetrovV/Python-Arkanoid-Breakout/HEAD/1.jpg -------------------------------------------------------------------------------- /screenshots/screen.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StanislavPetrovV/Python-Arkanoid-Breakout/HEAD/screenshots/screen.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-Arkanoid-Breakout 2 | Simplest version of Arkanoid 3 | 4 | ![alt text](screenshots/screen.jpg "Arkanoid") 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 StanislavPetrovV 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from random import randrange as rnd 3 | 4 | WIDTH, HEIGHT = 1200, 800 5 | fps = 60 6 | # paddle settings 7 | paddle_w = 330 8 | paddle_h = 35 9 | paddle_speed = 15 10 | paddle = pygame.Rect(WIDTH // 2 - paddle_w // 2, HEIGHT - paddle_h - 10, paddle_w, paddle_h) 11 | # ball settings 12 | ball_radius = 20 13 | ball_speed = 6 14 | ball_rect = int(ball_radius * 2 ** 0.5) 15 | ball = pygame.Rect(rnd(ball_rect, WIDTH - ball_rect), HEIGHT // 2, ball_rect, ball_rect) 16 | dx, dy = 1, -1 17 | # blocks settings 18 | block_list = [pygame.Rect(10 + 120 * i, 10 + 70 * j, 100, 50) for i in range(10) for j in range(4)] 19 | color_list = [(rnd(30, 256), rnd(30, 256), rnd(30, 256)) for i in range(10) for j in range(4)] 20 | 21 | pygame.init() 22 | sc = pygame.display.set_mode((WIDTH, HEIGHT)) 23 | clock = pygame.time.Clock() 24 | # background image 25 | img = pygame.image.load('1.jpg').convert() 26 | 27 | 28 | def detect_collision(dx, dy, ball, rect): 29 | if dx > 0: 30 | delta_x = ball.right - rect.left 31 | else: 32 | delta_x = rect.right - ball.left 33 | if dy > 0: 34 | delta_y = ball.bottom - rect.top 35 | else: 36 | delta_y = rect.bottom - ball.top 37 | 38 | if abs(delta_x - delta_y) < 10: 39 | dx, dy = -dx, -dy 40 | elif delta_x > delta_y: 41 | dy = -dy 42 | elif delta_y > delta_x: 43 | dx = -dx 44 | return dx, dy 45 | 46 | 47 | while True: 48 | for event in pygame.event.get(): 49 | if event.type == pygame.QUIT: 50 | exit() 51 | sc.blit(img, (0, 0)) 52 | # drawing world 53 | [pygame.draw.rect(sc, color_list[color], block) for color, block in enumerate(block_list)] 54 | pygame.draw.rect(sc, pygame.Color('darkorange'), paddle) 55 | pygame.draw.circle(sc, pygame.Color('white'), ball.center, ball_radius) 56 | # ball movement 57 | ball.x += ball_speed * dx 58 | ball.y += ball_speed * dy 59 | # collision left right 60 | if ball.centerx < ball_radius or ball.centerx > WIDTH - ball_radius: 61 | dx = -dx 62 | # collision top 63 | if ball.centery < ball_radius: 64 | dy = -dy 65 | # collision paddle 66 | if ball.colliderect(paddle) and dy > 0: 67 | dx, dy = detect_collision(dx, dy, ball, paddle) 68 | # if dx > 0: 69 | # dx, dy = (-dx, -dy) if ball.centerx < paddle.centerx else (dx, -dy) 70 | # else: 71 | # dx, dy = (-dx, -dy) if ball.centerx >= paddle.centerx else (dx, -dy) 72 | # collision blocks 73 | hit_index = ball.collidelist(block_list) 74 | if hit_index != -1: 75 | hit_rect = block_list.pop(hit_index) 76 | hit_color = color_list.pop(hit_index) 77 | dx, dy = detect_collision(dx, dy, ball, hit_rect) 78 | # special effect 79 | hit_rect.inflate_ip(ball.width * 3, ball.height * 3) 80 | pygame.draw.rect(sc, hit_color, hit_rect) 81 | fps += 2 82 | # win, game over 83 | if ball.bottom > HEIGHT: 84 | print('GAME OVER!') 85 | exit() 86 | elif not len(block_list): 87 | print('WIN!!!') 88 | exit() 89 | # control 90 | key = pygame.key.get_pressed() 91 | if key[pygame.K_LEFT] and paddle.left > 0: 92 | paddle.left -= paddle_speed 93 | if key[pygame.K_RIGHT] and paddle.right < WIDTH: 94 | paddle.right += paddle_speed 95 | # update screen 96 | pygame.display.flip() 97 | clock.tick(fps) --------------------------------------------------------------------------------