Created
March 18, 2021 16:09
-
-
Save Evolution0/de3746d7134caba3697e658cf2166089 to your computer and use it in GitHub Desktop.
prototype breakout clone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Breakout clone | |
import sys | |
import time | |
import pygame | |
import pygame.gfxdraw | |
display_width = 800 | |
display_height = 600 | |
black = (0, 0, 0) | |
grey = (100, 100, 100) | |
white = (255, 255, 255) | |
pygame.init() | |
gameDisplay = pygame.display.set_mode((display_width, display_height)) | |
pygame.display.set_caption('Breakout Clone') | |
clock = pygame.time.Clock() | |
def bricks_broken(count): | |
font = pygame.font.Font('resources/freesansbold.ttf', 25) | |
text = font.render("Score: " + str(count), True, black) | |
gameDisplay.blit(text, (0, 0)) | |
def bricks(brickx, bricky, clr): | |
pygame.draw.rect(gameDisplay, clr, [brickx, bricky, 80, 30]) | |
def paddle(x, y): | |
pygame.draw.rect(gameDisplay, black, [x, y, 100, 30]) | |
def ball(x, y): | |
pygame.gfxdraw.filled_circle(gameDisplay, x, y, 10, black) | |
def text_objects(text, font): | |
textsurface = font.render(text, True, black) | |
return textsurface, textsurface.get_rect() | |
def message_display(text): | |
largetext = pygame.font.Font('resources/freesansbold.ttf', 64) | |
textsurf, textrect = text_objects(text, largetext) | |
textrect.center = ((display_width / 2), (display_height / 2)) | |
gameDisplay.blit(textsurf, textrect) | |
pygame.display.update() | |
time.sleep(2) | |
game_intro() | |
def ballsout(): | |
message_display('Game Over') | |
def button(msg, x, y, w, h, ic, ac, action=None): | |
mouse = pygame.mouse.get_pos() | |
click = pygame.mouse.get_pressed() | |
if x + w > mouse[0] > x and y + h > mouse[1] > y: | |
pygame.draw.rect(gameDisplay, ac, (x, y, w, h)) | |
if click[0] == 1 and action is not None: | |
action() | |
else: | |
pygame.draw.rect(gameDisplay, ic, (x, y, w, h)) | |
smalltext = pygame.font.Font('resources/freesansbold.ttf', 20) | |
textsurf, textrect = text_objects(msg, smalltext) | |
textrect.center = ((x + (w / 2)), (y + (h / 2))) | |
gameDisplay.blit(textsurf, textrect) | |
def quitgame(): | |
pygame.quit() | |
sys.exit() | |
def game_intro(): | |
intro = True | |
while intro: | |
for event in pygame.event.get(): | |
# print(event) | |
if event.type == pygame.QUIT: | |
pygame.quit() | |
sys.exit() | |
gameDisplay.fill(white) | |
largetext = pygame.font.Font('resources/freesansbold.ttf', 64) | |
textsurf, textrect = text_objects("Breakout Clone", largetext) | |
textrect.center = ((display_width / 2), (display_height / 2)) | |
gameDisplay.blit(textsurf, textrect) | |
button("Start", 150, 450, 100, 50, white, grey, game_loop) | |
button("Quit", 550, 450, 100, 50, white, grey, quitgame) | |
pygame.display.update() | |
clock.tick(15) | |
def game_loop(): | |
paddlew = 100 | |
paddleh = 30 | |
x = (display_width / 2 - paddlew / 2) | |
y = (display_height - paddleh - 10) | |
ballx = (display_width / 2 - 5) | |
bally = 100 | |
ball_vector = 1 | |
x_change = 0 | |
ball_count = 3 | |
broken = 0 | |
gameexit = False | |
while not gameexit: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
pygame.quit() | |
sys.exit() | |
if event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_LEFT: | |
x_change = -5 | |
elif event.key == pygame.K_RIGHT: | |
x_change = 5 | |
if event.type == pygame.KEYUP and (event.key == pygame.K_LEFT or pygame.K_RIGHT): | |
x_change = 0 | |
x += x_change | |
if x > 800 - paddlew: | |
x = 800 - paddlew | |
elif x < 0: | |
x = 0 | |
gameDisplay.fill(white) | |
# Draw bricks | |
brickx = display_width / 2 - 40 | |
bricky = 10 | |
bricks(brickx, bricky, grey) | |
if bally >= 550 and ballx >= x: | |
ball_vector = 0 | |
elif bally <= 0 - 50: | |
ball_vector = 2 | |
elif bally <= 40 and brickx >= ballx: | |
ball_vector = 1 | |
if ball_vector == 0: | |
bally -= 4 | |
elif ball_vector == 2: | |
bally = 100 | |
ballx = (display_width / 2 - 5) | |
ball_count -= 1 | |
ball_vector = 1 | |
else: | |
bally += 4 | |
if ball_count == 0: | |
ballsout() | |
ball(ballx, bally) | |
paddle(x, y) | |
bricks_broken(broken) | |
# Add ball collision and brick/paddle bounce/break logic | |
""" | |
if thing_starty > display_height: | |
thing_starty = 0 - thing_height | |
thing_startx = random.randrange(0, display_width) | |
dodged += 1 | |
thing_speed += 1 | |
thing_width += (dodged * 1.2) | |
if y < thing_starty + thing_height: | |
print("Y-Axis Collision") | |
if thing_startx < x < thing_startx + thing_width or thing_startx < x + car_width < thing_startx + thing_width: | |
print('X-Axis Collision') | |
""" | |
pygame.display.update() | |
clock.tick(60) | |
game_intro() | |
game_loop() | |
pygame.quit() | |
sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment