top of page

소스 코드 제출

공개·회원 69명

20251018

import pygame

import random

import time


pygame.init()


BLACK = (0, 0, 0)

RED = (255, 0, 0)

GREEN = (0, 255, 0)

BLUE = (0, 0, 255)

WHITE = (255, 255, 255)

YELLOW = (255, 255, 0)

large_font = pygame.font.SysFont(None, 72)

small_font = pygame.font.SysFont(None, 36)

screen_width = 600

screen_height = 800

screen = pygame.display.set_mode((screen_width, screen_height))


clock = pygame.time.Clock()


def start():

global game_start

game_start=1


def text_objects(text, font):

textSurface = font.render(text, True, GREEN)

return textSurface, textSurface.get_rect()


def button(txt,x,y,w,h,ic,ac,action):

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(screen,ac,(x,y,w,h))

if click[0]==1 and action!=None:

action()

else :

pygame.draw.rect(screen,ic,(x,y,w,h))


smallText=pygame.font.SysFont("Arial",20)

textSurf,textRect=text_objects(txt,smallText)

textRect.center=((x+(w/2)), (y+(h/2)))

screen.blit(textSurf,textRect)


def runGame():

score = 0

missed = 0

SUCCESS = 1

FAILURE = 2

game_over = 0

game_start = 0

bricks = []

COLUMN_COUNT = 8

ROW_COUNT = 7

for column_index in range(COLUMN_COUNT):

for row_index in range(ROW_COUNT):

brick = pygame.Rect(column_index * (60 + 10) + 35, row_index * (16 + 5) + 35, 60, 16)

bricks.append(brick)


ball = pygame.Rect(screen_width // 2 - 16 // 2, screen_height // 2 - 16 // 2, 16, 16)

ball_dx = 5

ball_dy = -5


ball2 = pygame.Rect(screen_width // 2 - 200 // 2, screen_height // 2 - 16 // 2, 16, 16)

ball2_dx = 5

ball2_dy = -5


paddle = pygame.Rect(screen_width // 2 - 80 // 2, screen_height - 16, 80, 16)

paddle_dx = 0

screen.fill(BLACK)


while game_start == 0:

clock.tick(30)

myFont = pygame.font.SysFont("Arial", 20, True, False)

screen.blit(myFont.render(f'press S to start', True, 'yellow'), (220, 400, 0, 0))


mFont = pygame.font.SysFont("Arial", 70, True, False)

screen.blit(mFont.render(f'BRICKSBREAK', True, 'green'), (35, 300, 0, 0))


for event in pygame.event.get():

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_s:

game_start = 1


pygame.display.update()


while game_start == 1:

clock.tick(50 + score)

screen.fill(BLACK)


for event in pygame.event.get():

if event.type == pygame.QUIT:

break

elif event.type == pygame.KEYDOWN:

if event.key == pygame.K_LEFT:

paddle_dx = -5

elif event.key == pygame.K_RIGHT:

paddle_dx = 5

elif event.type == pygame.KEYUP:

if event.key == pygame.K_LEFT:

paddle_dx = 0

elif event.key == pygame.K_RIGHT:

paddle_dx = 0


paddle.left += paddle_dx


ball.left += ball_dx

ball.top += ball_dy


if game_over == 0 :

if ball.left <= 0:

ball.left = 0

ball_dx = -ball_dx

elif ball.left >= screen_width - ball.width:

ball.left = screen_width - ball.width

ball_dx = -ball_dx

if ball.top < 0:

ball.top = 0

ball_dy = -ball_dy

elif ball.top >= screen_height:

missed += 1

ball.left = screen_width // 2 - ball.width // 2

ball.top = screen_height // 2 - ball.width // 2

ball_dy = -ball_dy


if score >= 40 and game_over == 0:

ball2.left += ball2_dx

ball2.top += ball2_dy


if ball2.left <= 0:

ball2.left = 0

ball2_dx = -ball2_dx

elif ball2.left >= screen_width - ball2.width:

ball2.left = screen_width - ball2.width

ball2_dx = -ball2_dx

if ball2.top < 0:

ball2.top = 0

ball2_dy = -ball2_dy

elif ball2.top >= screen_height:

missed += 1

ball2.left = screen_width // 2 - ball2.width // 2

ball2.top = screen_height // 2 - ball2.width // 2

ball2_dy = -ball2_dy


if missed >= 5:

game_over = FAILURE


if paddle.left < 0:

paddle.left = 0

elif paddle.left > screen_width - paddle.width:

paddle.left = screen_width - paddle.width


for brick in bricks:

if score >= 40:

if ball.colliderect(brick) :

bricks.remove(brick)

ball_dy = -ball_dy

score += 1

break

if ball2.colliderect(brick):

bricks.remove(brick)

ball2_dy = -ball2_dy

score += 1

break

else:

if ball.colliderect(brick):

bricks.remove(brick)

ball_dy = -ball_dy

score += 1

break


if ball.colliderect(paddle):

ball_dy = -ball_dy

if ball.centerx <= paddle.left or ball.centerx > paddle.right:

ball_dx = ball_dx * -1


if score >= 40:

if ball2.colliderect(paddle):

ball2_dy = -ball2_dy

if ball2.centerx <= paddle.left or ball2.centerx > paddle.right:

ball2_dx = ball2_dx * -1


if len(bricks) == 0:

game_over = SUCCESS


# 화면 그리기


for brick in bricks:

pygame.draw.rect(screen, GREEN, brick)


if game_over == 0:

pygame.draw.circle(screen, WHITE, (ball.centerx, ball.centery), ball.width // 2)

if score >= 40:

pygame.draw.circle(screen, WHITE, (ball2.centerx, ball2.centery), ball2.width // 2)


pygame.draw.rect(screen, BLUE, paddle)


if score >= 40:

score_image = small_font.render('Point {}'.format(score), True, RED)

screen.blit(score_image, (10, 10))

else:

score_image = small_font.render('Point {}'.format(score), True, YELLOW)

screen.blit(score_image, (10, 10))


missed_image = small_font.render('Missed {}'.format(missed), True, YELLOW)

screen.blit(missed_image, missed_image.get_rect(right=screen_width - 10, top=10))


if game_over > 0:

if game_over == SUCCESS:

success_image = large_font.render('success', True, RED)

screen.blit(success_image,

success_image.get_rect(centerx=screen_width // 2, centery=screen_height // 2))

elif game_over == FAILURE:

failure_image = large_font.render('fail', True, RED)

screen.blit(failure_image,

failure_image.get_rect(centerx=screen_width // 2, centery=screen_height // 2))

myFont = pygame.font.SysFont("Arial", 20, True, False)

button("RESTART", 150, 450, 100, 50, RED, YELLOW, runGame)

button("QUIT", 340, 450, 100, 50, RED, YELLOW, pygame.quit)

pygame.display.update()

runGame()

pygame.quit()


# 순위표 만들기

15회 조회
주소 : 경기도 용인시 광교중앙로 302 블루 스퀘어 602호
연락처 : 031) 216 - 1546 ,     031) 215 - 1546
사업자등록번호 : 465-92-00916
​학원 등록 제 4603호
bottom of page