# import pygame
# from pygame.rect import *
# import random
#
# #########################################################
# def restart():
# global score, isGameOver
# for i in range(len(star)):
# rectStar[i].y = -1
# score = 0
# isGameOver = False
#
# def continue1():
# global score, isGameOver
# for i in range(len(star)):
# rectStar[i].y = -1
# isGameOver = False
#
# def eventProcess(move):
# global isActive
# for event in pygame.event.get():
# if event.type == pygame.KEYDOWN:
# if event.key == pygame.K_ESCAPE:
# pygame.quit()
# isActive = False
# if event.key == pygame.K_LEFT:
# move.x = -1
# if event.key == pygame.K_RIGHT:
# move.x = 1
# if event.key == pygame.K_UP:
# move.y = -1
# if event.key == pygame.K_DOWN:
# move.y = 1
# if event.key == pygame.K_r:
# restart()
# if event.key == pygame.K_c:
# continue1()
# #########################################################
# def movePlayer(player, current, move, isGameOver):
# global SCREEN_WIDTH, SCREEN_HEIGHT
# if not isGameOver:
# current.x += move.x
# current.y += move.y
# #### limit
# if current.y > SCREEN_HEIGHT - current.height:
# current.y = SCREEN_HEIGHT - current.height
# if current.x > SCREEN_WIDTH-current.width:
# current.x = SCREEN_WIDTH - current.width
# if current.y < 0:
# current.y = 0
# if current.x < 0:
# current.x = 0
# SCREEN.blit(player, current)
# #########################################################
# def timeUpdate50ms():
# global time500ms
# time500ms += 1
# if time500ms > 5:
# time500ms = 0
# return True
# return False
#
# def makeStar(rec):
# if timeUpdate50ms():
# idex = random.randint(0, len(star)-1)
# if rec[idex].y == -1:
# rec[idex].x = random.randint(0, SCREEN_WIDTH)
# rec[idex].y = 0
#
# def moveStar(star, current, isGameOver):
# global SCREEN_HEIGHT
# for i in range(len(star)):
# if current[i].y == -1:
# continue
# if not isGameOver:
# current[i].y += 1
# if current[i].y > SCREEN_HEIGHT:
# current[i].y = -1
# SCREEN.blit(star[i], current[i])
# #########################################################
# def CheckCollision(player, star):
# global isGameOver, score
# if isGameOver:
# return
# for rec in star:
# if rec.top < player.bottom \
# and player.top < rec.bottom \
# and rec.left < (player.right-8) \
# and (player.left+8) < rec.right:
# isGameOver = True
# break
# score += 1
# #########################################################
# def timeUpdate4sec(isGameOver):
# global time4Sec, time4SecToggle
# if not isGameOver:
# return False
# time4Sec += 1
# if time4Sec > 40:
# time4Sec = 0
# time4SecToggle = (~time4SecToggle)
# return time4SecToggle
#
# def setText(isupdate=False):
# global score
# myFont = pygame.font.SysFont("arial", 20, True, False)
#
# SCREEN.blit(myFont.render(
# f'score : {score}', True, 'green'), (10, 10, 0, 0))
#
# if isupdate:
# SCREEN.blit(myFont.render(
# f'Game Over!!', True, 'red'), (150, 300, 0, 0))
# SCREEN.blit(myFont.render(
# f'press R - Restart', True, 'red'), (140, 320, 0, 0))
# SCREEN.blit(myFont.render(
# f'press C - continue', True, 'red'), (135, 340, 0, 0))
# #########################################################
# #########################################################
#
#
# ##1. 변수 선언
# SCREEN_WIDTH = 400
# SCREEN_HEIGHT = 600
# score = 0
# isActive = True
# isGameOver = False
# move = Rect(0, 0, 0, 0)
# time500ms = 0
# time4Sec = 0
# time4SecToggle = False
# #########################################################
# ##2. 스크린
# pygame.init()
# SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# pygame.display.set_caption("CodingNow!!")
# #########################################################
# ##3. player
# player = pygame.image.load("Player.png")
# player = pygame.transform.scale(player, (20, 30))
# rectPlayer = player.get_rect()
# rectPlayer.centerx = (SCREEN_WIDTH / 2)
# rectPlayer.centery = (SCREEN_HEIGHT / 2)
# #########################################################
# ##4. 유성
# star = [pygame.image.load("star.png") for i in range(20)]
# rectStar = [None for i in range(len(star))]
# for i in range(len(star)):
# star[i] = pygame.transform.scale(star[i], (20, 20))
# rectStar[i] = star[i].get_rect()
# rectStar[i].y = -1
# #########################################################
# ##5. time
# clock = pygame.time.Clock()
#
# while isActive:
# #1. 화면 검정색으로 지우기
# SCREEN.fill((0, 0, 0))
# #2. 이번트처리
# eventProcess(move)
# #3. 플레이어
# movePlayer(player, rectPlayer,move,isGameOver)
# #4. 유성만들기
# makeStar(rectStar)
# moveStar(star, rectStar,isGameOver)
# #5. 충돌
# CheckCollision(rectPlayer, rectStar)
# #6. Text 업데이트
# setText(timeUpdate4sec(isGameOver))
# #7.화면업데이트
# pygame.display.flip()
# clock.tick(100)
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 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 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:
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)
screen.blit(myFont.render(f'press q to quit', True, 'yellow'), (230, 450, 0, 0))
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
runGame()
pygame.display.update()
runGame()
pygame.quit()
종료 버튼 만들기
top of page

실제 작동 상태를 확인하려면 라이브 사이트로 이동하세요.
20251011(2)
20251011(2)
댓글 0개
좋아요
댓글(0)
bottom of page