20251021
# import pygame
#
# pygame.init()
#
# screen = pygame.display.set_mode((700, 800))
# pygame.display.set_caption("my_game")
#
#
# running = True
# while running:
# # 이벤트 처리
# for event in pygame.event.get():
# if event.type == pygame.QUIT:
# running = False
# if event.type == pygame.KEYDOWN: # 만약 키보드를 눌렀을 때
# if event.key == pygame.K_LEFT: # 만약 그 키가 왼쪽화살표 키일때
# character_dx = -5
# if event.key == pygame.K_RIGHT:
# character_dx = 5
#
# if event.type == pygame.KEYUP: # 만약 키보드를 땠을 때
# if event.key == pygame.K_LEFT: # 만약 그 키가 왼쪽화살표 키일때
# character_dx = 0
# if event.key == pygame.K_RIGHT:
# character_dx = 0
#
#
# # 화면 업데이트
# screen.fill((0, 0, 0))
#
# screen.blit(character, (character_x_pos, 0))
#
# pygame.display.flip()
#
# pygame.quit()
#
##임포트 받기
import pygame, sys
from pygame.locals import *
import random, time
##디스플레이 설정
pygame.init()
fps = 60
FramePerSec = pygame.time.Clock()
Surface = pygame.display.set_mode((700, 400))
# 색깔 설정
red = (255, 0, 0)
orange = (255, 153, 51)
yellow = (255, 255, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
white = (255, 255, 255)
black = (0, 0, 0)
##폰트, 글씿체 선택
font = pygame.font.SysFont('@System', 20)
small_font = pygame.font.SysFont('@System', 20)
game_over = font.render("GAME_OVER", True, black)
##기본 배경 설정
GameDisplay = pygame.display.set_mode((640, 440))
background = GameDisplay.fill(black)
##화면 설정
screen = pygame.display.set_mode((700, 800))
pygame.display.set_caption("my_game")
##랜덤 답 3자리 생성 함수
a="0"
b="0"
c="0"
while True:
A=random.randint(0,9)
B=random.randint(0,9)
C=random.randint(0,9)
if A == B or A==C or B == C :
continue
else :
break
answer=[A,B,C]
##변수 모아보기
strike=0
ball=0
out=0
tried=0
what=0
move=150
box=160
rectangle='rectangle.png'
class ResultBox(pygame.sprite.Sprite):
def __init__(self,j,i):
super().__init__()
self.image = pygame.image.load('rectangle.png')
self.rect = self.image.get_rect()
self.rect.center = (j, i)
# 변수 선언
text=""
##넘버 함수(입력 받은 guess 판정 & 결과 알려주기)
def number():
global strike
global ball
global out
global answer
global a, b, c, screen
global move
global guess
global tried
global box
# 변수를 리셋해버리자
tried +=1
ball = strike = out = 0
guess=[a, b, c]
for i in range(0, 3):
# print(answer)
# print(guess)
if answer[i] == guess[i]:
strike += 1
elif guess[i] == answer[i - 1] or guess[i] == answer[i - 2]:
ball += 1
elif guess[i] != answer[i - 1] or guess[i] != answer[i - 2]:
out += 1
# print("S : ", strike," B : ", ball,"O : ", out)
abc = map(str, guess)
if strike == 3:
# print(" HOMERUN!!!, ", tried,"회 도전만에 성공!")#화면모드로 고치기
pen = font.render("HOMERUN!!! " + "YOU TRIED " + str(tried) + "TH TIME!", True, black)
elif out == 3:
# print( "OUT!")#화면모드로 고치기
pen = font.render(' '.join(abc) + " == " + "OUT!", True, black)
else:
# print(guess + " ==> " , ball ,"BALL, " ,strike ," STRIKE")#화면모드로 고치기
pen = font.render(' '.join(abc) + " == " + str(ball) + "BALL, " + str(strike) + "STRIKE", True, black)
B1 = ResultBox(120, box) # box만들기
GameDisplay.blit(B1.image, B1.rect) # box 그리기
screen.blit(pen, [60, move]) # 결과 그리기(글자변수, 위치)
move += 50
box +=50
pygame.display.update()
##입력 사각형 그리기
def draw_num() :
# 화면 업데이트
# screen.fill((0, 0, 0)) # 배경색 검정색
# 입력 숫자 배경 사각형
pygame.draw.rect(screen, (255, 255, 255), [250, 10, 100, 100])
pygame.draw.rect(screen, (255, 255, 255), [10, 10, 100, 100])
pygame.draw.rect(screen, (255, 255, 255), [130, 10, 100, 100])
# 입력 숫자 텍스트 출력
font = pygame.font.SysFont(None, 30, False, False)
n1 = font.render(str(a), True, black, None)
n2 = font.render(str(b), True, black, None)
n3 = font.render(str(c), True, black, None)
screen.blit(n1, [50, 50])
screen.blit(n2, [170, 50])
screen.blit(n3, [290, 50])
running = True
##진행 + 디스플레이에 실행(화면에 출력)
while running:
# 이벤트 처리
if tried == 14 :
pen = font.render("YOU FAILED! ___ GAME OVER!! BAD LUCK>>", True, white)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN: # 만약 키다운 이벤트가 감지되었을 때
if event.key == pygame.K_BACKSPACE:
text = text[:-1]
else : ### 숫자, 스페이스들어왔을때만!!!!!!!!으로 바꾸기!!!!
text += event.unicode
# print(text)
screen.blit(pen, [400, 160])
if len(text)==1 :
a = int(text)
elif len(text)==3 :
a, b = map(int, text.split())
elif len(text)==5 :
a, b, c = map(int, text.split())
draw_num()
# 입력 완료==
if len(text)>=6 :
# 넘버 함수로 3자리 guess 판정
number()
#입력받을 세자리 저장 변수 리셋
text=""
pygame.display.flip()
pygame.quit()
# text = ""
# text = "3"
# text = "3 4"
# text = "3 4 5"
# text = "3 4 5 "




