From b4bc71031313cecc56b7d08cbaaa4ac90a39dd87 Mon Sep 17 00:00:00 2001 From: parkjeonghyeon1013 Date: Wed, 27 Sep 2023 17:43:33 +0900 Subject: [PATCH 1/4] for go home commit G-baseballGame.py - 0927 --- G-baseballGame.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 G-baseballGame.py diff --git a/G-baseballGame.py b/G-baseballGame.py new file mode 100644 index 0000000000000..f4b956195baa5 --- /dev/null +++ b/G-baseballGame.py @@ -0,0 +1,42 @@ +import sys +import pygame +import random +from pygame.locals import * + +pygame.init() +WIDTH = 800 +HEIGHT = 1000 +screen = pygame.display.set_mode([WIDTH, HEIGHT]) +fps = 60 +timer = pygame.time.Clock() +font = pygame.font.Font('freesansbold.ttf', 18) +pygame.display.set_caption('Say Number') + +class Button: + def __init__(self, text, x_pos, y_pos, enabled): + self.text = text + self.x_pos = x_pos + self.y_pos = y_pos + self.enabled = enabled + + def draw(self): + button_text = font.render(self.text, True, 'black') + button_rect = pygame.rect.Rect((self.x_pos, self.y_pos), (200,25)) + pygame.draw.rect(screen, 'gray', button_rect, 0,5) + + + + + +run = True +while run: + screen.fill('white') + timer.tick(fps) + my_button = Button() + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + run = False + + pygame.display.flip() +pygame.quit() From 0b8c43ed80544cecbf4c59ac2f8dff7913a786a1 Mon Sep 17 00:00:00 2001 From: parkjeonghyeon1013 Date: Wed, 4 Oct 2023 17:31:52 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=EC=88=AB=EC=9E=90=20=EC=95=BC=EA=B5=AC?= =?UTF-8?q?=EA=B2=8C=EC=9E=84=20-=20=EC=82=AC=EC=9A=A9=EC=9E=90=EA=B0=80?= =?UTF-8?q?=20=EC=B6=94=EC=B8=A1=ED=95=9C=20=EC=88=AB=EC=9E=90=EB=A5=BC=20?= =?UTF-8?q?=EB=88=84=EB=A5=BC=20=EC=88=98=20=EC=9E=88=EB=8A=94=20=EB=B2=84?= =?UTF-8?q?=ED=8A=BC=EC=9D=84=20=EB=A7=8C=EB=93=A4=EC=97=88=EC=8A=B5?= =?UTF-8?q?=EB=8B=88=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- G-baseballGame.py | 167 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 142 insertions(+), 25 deletions(-) diff --git a/G-baseballGame.py b/G-baseballGame.py index f4b956195baa5..75bef87b877a1 100644 --- a/G-baseballGame.py +++ b/G-baseballGame.py @@ -3,40 +3,157 @@ import random from pygame.locals import * + pygame.init() -WIDTH = 800 -HEIGHT = 1000 -screen = pygame.display.set_mode([WIDTH, HEIGHT]) -fps = 60 -timer = pygame.time.Clock() -font = pygame.font.Font('freesansbold.ttf', 18) -pygame.display.set_caption('Say Number') -class Button: - def __init__(self, text, x_pos, y_pos, enabled): - self.text = text - self.x_pos = x_pos - self.y_pos = y_pos - self.enabled = enabled - def draw(self): - button_text = font.render(self.text, True, 'black') - button_rect = pygame.rect.Rect((self.x_pos, self.y_pos), (200,25)) - pygame.draw.rect(screen, 'gray', button_rect, 0,5) +screen_width = 1000 +screen_height = 600 + +screen = pygame.display.set_mode((screen_width, screen_height)) +pygame.display.set_caption('Baseball Game') + +font = pygame.font.SysFont('Constantia', 30) + +#define colours +bg = (204, 102, 0) +red = (255, 0, 0) +black = (0, 0, 0) +white = (255, 255, 255) + +#define global variable +clicked = False +counter = 0 + +class button(): + + #colours for button and text + button_col = (255, 0, 0) + hover_col = (75, 225, 255) + click_col = (50, 150, 255) + text_col = black + width = 100 + height = 70 + + def __init__(self, x, y, text): + self.x = x + self.y = y + self.text = text + + def draw_button(self): + + global clicked + action = False + + #get mouse position + pos = pygame.mouse.get_pos() + + #create pygame Rect object for the button + button_rect = Rect(self.x, self.y, self.width, self.height) + + #check mouseover and clicked conditions + if button_rect.collidepoint(pos): + if pygame.mouse.get_pressed()[0] == 1: + clicked = True + pygame.draw.rect(screen, self.click_col, button_rect) + elif pygame.mouse.get_pressed()[0] == 0 and clicked == True: + clicked = False + action = True + else: + pygame.draw.rect(screen, self.hover_col, button_rect) + else: + pygame.draw.rect(screen, self.button_col, button_rect) + + #add shading to button + pygame.draw.line(screen, white, (self.x, self.y), (self.x + self.width, self.y), 2) + pygame.draw.line(screen, white, (self.x, self.y), (self.x, self.y + self.height), 2) + pygame.draw.line(screen, black, (self.x, self.y + self.height), (self.x + self.width, self.y + self.height), 2) + pygame.draw.line(screen, black, (self.x + self.width, self.y), (self.x + self.width, self.y + self.height), 2) + + #add text to button + text_img = font.render(self.text, True, self.text_col) + text_len = text_img.get_width() + screen.blit(text_img, (self.x + int(self.width / 2) - int(text_len / 2), self.y + 25)) + return action +# 랜덤 변수 생성 +answer = random.randrange(100,900,1) +# 사용자 입력 숫자의 총합 +checkPlace = 0 # 사용자가 누른 숫자의 자릿수 체크 +playtime = 0 # 사용자가 숫자를 맞춘 횟수 +place1 = 0 +place10 = 0 +place100 = 0 +totalNum = 0 +# 사용자 입력 버튼 변수 +one = button(600, 200, '1') +two = button(700, 200, '2') +three = button(800, 200, '3') +four = button(600, 300, '4') +five = button(700, 300, '5') +six = button(800, 300, '6') +seven = button(600, 400, '7') +eight = button(700, 400, '8') +nine = button(800, 400, '9') +zero = button(700, 500, '0') +deleteNum = button(600, 500, 'Del') +checkNum = button(800, 500, 'Check') +# 출력창 변수 +myFont = pygame.font.SysFont(None, 50) #(글자체, 글자크기) None=기본글자체 + run = True while run: - screen.fill('white') - timer.tick(fps) - my_button = Button() - for event in pygame.event.get(): - if event.type == pygame.QUIT: - run = False + screen.fill(bg) + # 사용자 입력용 숫자 키패드 + if one.draw_button(): + print('Two') + if two.draw_button(): + print('Two') + if three.draw_button(): + print('Two') + if four.draw_button(): + print('Two') + if five.draw_button(): + print('Two') + if six.draw_button(): + print('Two') + if seven.draw_button(): + print('Two') + if eight.draw_button(): + print('Two') + if nine.draw_button(): + print('Two') + if zero.draw_button(): + print('Two') + if deleteNum.draw_button(): + print('Two') + if checkNum.draw_button(): + print('Two') + +# render함수로 사용자가 누른 숫자 출력(문자열이 아니면 str로 변환해야함) + #myText = myFont.render("Hello World " + True, (0,0,255)) #(Text,anti-alias, color) + + counter_img = font.render(str(counter), True, red) + screen.blit(counter_img, (280, 450)) + #screen.blit(myText, (100,100)) #(글자변수, 위치) + + + + + + + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + run = False + + + pygame.display.update() + - pygame.display.flip() -pygame.quit() +pygame.quit() \ No newline at end of file From 5aafcea451d23efec6af32491139fb46e4d7c898 Mon Sep 17 00:00:00 2001 From: JeongHyeon Park Date: Tue, 10 Oct 2023 08:51:16 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=ED=85=8D=EC=8A=A4=ED=8A=B8=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- G-baseballGame.py | 57 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/G-baseballGame.py b/G-baseballGame.py index 75bef87b877a1..62bbf6842bc1f 100644 --- a/G-baseballGame.py +++ b/G-baseballGame.py @@ -16,14 +16,36 @@ font = pygame.font.SysFont('Constantia', 30) #define colours -bg = (204, 102, 0) +bg = (0, 102, 0) red = (255, 0, 0) black = (0, 0, 0) white = (255, 255, 255) +# 랜덤 변수 생성 +answer = random.randrange(100,900,1) + #define global variable clicked = False -counter = 0 +counter = answer + + +class panel(): + #colours for button and text + button_col = (255, 0, 0) + hover_col = (75, 225, 255) + click_col = (50, 150, 255) + text_col = black + width = 100 + height = 70 + + def __init__(self,x,y,answer): + self.x = x + self.y = y + self.answer = answer + + def draw_panel(self): + panel_rect = Rect(self.x, self.y, self.width, self.height) + class button(): @@ -77,8 +99,6 @@ def draw_button(self): return action -# 랜덤 변수 생성 -answer = random.randrange(100,900,1) # 사용자 입력 숫자의 총합 checkPlace = 0 # 사용자가 누른 숫자의 자릿수 체크 @@ -103,15 +123,25 @@ def draw_button(self): checkNum = button(800, 500, 'Check') # 출력창 변수 +panel_1 = panel(600,200,'1') + + myFont = pygame.font.SysFont(None, 50) #(글자체, 글자크기) None=기본글자체 run = True while run: - screen.fill(bg) + for turn in range(5): + '' + + + + + + # 사용자 입력용 숫자 키패드 if one.draw_button(): - print('Two') + print(answer) if two.draw_button(): print('Two') if three.draw_button(): @@ -133,13 +163,25 @@ def draw_button(self): if deleteNum.draw_button(): print('Two') if checkNum.draw_button(): - print('Two') + userNum = '사용자가 누르는 숫자 123' + counter_img = font.render('ddd', True, red) + screen.blit(counter_img, (100, 100)) + pygame.display.update() #모든 화면 그리기 업데이트 # render함수로 사용자가 누른 숫자 출력(문자열이 아니면 str로 변환해야함) #myText = myFont.render("Hello World " + True, (0,0,255)) #(Text,anti-alias, color) + + if panel_1.draw_panel(): + print('Yessssssssssssssssssssssssssssssssssss') + + + + counter_img = font.render(str(counter), True, red) screen.blit(counter_img, (280, 450)) + counter_img = font.render('Yessssssssssss', True, red) + screen.blit(counter_img, (100, 100)) #screen.blit(myText, (100,100)) #(글자변수, 위치) @@ -147,7 +189,6 @@ def draw_button(self): - for event in pygame.event.get(): if event.type == pygame.QUIT: run = False From e360942d015e1c5708da632401a956db127378a3 Mon Sep 17 00:00:00 2001 From: JeongHyeon Park Date: Tue, 10 Oct 2023 22:27:59 +0900 Subject: [PATCH 4/4] =?UTF-8?q?G-baseballGame=20=ED=8C=8C=EC=9D=BC?= =?UTF-8?q?=EA=B3=BC=20G-memoryGame=20=ED=8C=8C=EC=9D=BC=EC=9D=84=20?= =?UTF-8?q?=EC=BB=A4=EB=B0=8B=ED=95=A9=EB=8B=88=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- G-baseballGame.py | 43 ++++++--- G-memoryGame.py | 236 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 266 insertions(+), 13 deletions(-) create mode 100644 G-memoryGame.py diff --git a/G-baseballGame.py b/G-baseballGame.py index 62bbf6842bc1f..bb7cfbfa99a14 100644 --- a/G-baseballGame.py +++ b/G-baseballGame.py @@ -27,7 +27,9 @@ #define global variable clicked = False counter = answer - +firstNum = 0 +secondNum = 0 +thirdNum = 0 class panel(): #colours for button and text @@ -69,6 +71,7 @@ def draw_button(self): #get mouse position pos = pygame.mouse.get_pos() + #create pygame Rect object for the button button_rect = Rect(self.x, self.y, self.width, self.height) @@ -78,6 +81,7 @@ def draw_button(self): if pygame.mouse.get_pressed()[0] == 1: clicked = True pygame.draw.rect(screen, self.click_col, button_rect) + self.append_value(firstNum) elif pygame.mouse.get_pressed()[0] == 0 and clicked == True: clicked = False action = True @@ -98,7 +102,18 @@ def draw_button(self): screen.blit(text_img, (self.x + int(self.width / 2) - int(text_len / 2), self.y + 25)) return action - + def append_value(self,new_value): + # Set a limit of values that can be entered + if self.text.get_width() < 260: + if self.value == "0" or self.new_entry: + self.value = new_value + self.new_entry = False + else: + self.value += new_value + + self.text = self.font.render(self.value,True,BLACK) + # Set key_pressed as true + self.key_pressed = True # 사용자 입력 숫자의 총합 checkPlace = 0 # 사용자가 누른 숫자의 자릿수 체크 @@ -141,31 +156,33 @@ def draw_button(self): # 사용자 입력용 숫자 키패드 if one.draw_button(): - print(answer) + firstNum = 1 + append + if two.draw_button(): - print('Two') + firstNum = 2 if three.draw_button(): - print('Two') + firstNum = 3 if four.draw_button(): - print('Two') + firstNum = 4 if five.draw_button(): - print('Two') + firstNum = 5 if six.draw_button(): - print('Two') + firstNum = 6 if seven.draw_button(): - print('Two') + firstNum = 7 if eight.draw_button(): - print('Two') + firstNum = 8 if nine.draw_button(): - print('Two') + firstNum = 9 if zero.draw_button(): - print('Two') + firstNum = 0 if deleteNum.draw_button(): print('Two') if checkNum.draw_button(): userNum = '사용자가 누르는 숫자 123' counter_img = font.render('ddd', True, red) - screen.blit(counter_img, (100, 100)) + screen.blit(counter_img, (280, 450)) pygame.display.update() #모든 화면 그리기 업데이트 # render함수로 사용자가 누른 숫자 출력(문자열이 아니면 str로 변환해야함) diff --git a/G-memoryGame.py b/G-memoryGame.py new file mode 100644 index 0000000000000..67f7b22131e07 --- /dev/null +++ b/G-memoryGame.py @@ -0,0 +1,236 @@ +# the imports +import pygame +import random +from itertools import product +from pygame.locals import * +from pygame.color import Color + + +# the constants +FPS = 30 +SCREEN_WIDTH = 800 +SCREEN_HEIGHT = 400 +SQUARE_SIZE = 50 +SQUARE_GAP = 10 +BOARD_WIDTH = 8 +BOARD_HEIGHT = 4 +X_MARGIN = (SCREEN_WIDTH - (BOARD_WIDTH * (SQUARE_SIZE + SQUARE_GAP))) // 2 +Y_MARGIN = (SCREEN_HEIGHT - (BOARD_HEIGHT * (SQUARE_SIZE + SQUARE_GAP))) // 2 + +# the board size must be even due to pairs +assert (BOARD_HEIGHT * BOARD_WIDTH) % 2 == 0, 'The board size must be even' + +# the shapes +DIAMOND = 'diamond' +SQUARE = 'square' +TRIANGLE = 'triangle' +CIRCLE = 'circle' + +BGCOLOR = Color('blue') + +# the main function +def main(): + global screen, clock + + pygame.init() + + screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) + pygame.display.set_caption('Memory game') + + clock = pygame.time.Clock() + + shape = (DIAMOND, SQUARE, TRIANGLE, CIRCLE) + colors = (Color('cyan'), Color('magenta'), Color('gray'), Color('chocolate')) + + # There should be enough symbols + assert len(shape) * len(colors) >= BOARD_HEIGHT * BOARD_WIDTH // 2, 'There are not sufficient icons' + + board = get_random_board(shape, colors) + revealed = [[False] * BOARD_WIDTH for i in range(BOARD_HEIGHT)] # keeps track of visibility of square + + mouse_x = None + mouse_y = None + mouse_clicked = False + first_selection = None + + running = True + start_game_animation(board) + + while running: + screen.fill(BGCOLOR) + draw_board(board, revealed) + + for event in pygame.event.get(): + if event.type == QUIT: + running = False + elif event.type == MOUSEMOTION: + mouse_x, mouse_y = pygame.mouse.get_pos() + elif event.type == MOUSEBUTTONDOWN: + mouse_x, mouse_y = pygame.mouse.get_pos() + mouse_clicked = True + + x, y = get_pos(mouse_x, mouse_y) + + if x is not None and y is not None: + if not revealed[x][y]: + if mouse_clicked: + revealed[x][y] = True + draw_square(board, revealed, x, y) + + if first_selection is None: + first_selection = (x, y) + else: + pygame.time.wait(1000) + if board[x][y] != board[first_selection[0]][first_selection[1]]: + revealed[x][y] = False + revealed[first_selection[0]][first_selection[1]] = False + first_selection = None + + if game_won(revealed): + + game_won_animation(board, revealed) + + board = get_random_board(shape, colors) + revealed = [[False] * BOARD_WIDTH for i in range(BOARD_HEIGHT)] + start_game_animation(board) + + else: + draw_select_box(x, y) + + mouse_clicked = False + pygame.display.update() + + +def game_won(revealed): + """ Returns whether all squares are found""" + + return all(all(x) for x in revealed) + + +def game_won_animation(board, revealed): + """ Flashes background colors when the game is won""" + + color1 = Color('cyan') + color2 = BGCOLOR + for i in range(10): + color1, color2 = color2, color1 + screen.fill(color1) + draw_board(board, revealed) + pygame.display.update() + pygame.time.wait(300) + + +def start_game_animation(board): + """Starts game by randomly showing 5 squares""" + + coordinates = list(product(range(BOARD_HEIGHT), range(BOARD_WIDTH))) + random.shuffle(coordinates) + + revealed = [[False] * BOARD_WIDTH for i in range(BOARD_HEIGHT)] + + screen.fill(BGCOLOR) + draw_board(board, revealed) + pygame.display.update() + pygame.time.wait(500) + + for sz in range(0, BOARD_HEIGHT * BOARD_WIDTH, 5): + l = coordinates[sz: sz + 5] + for x in l: + revealed[x[0]][x[1]] = True + draw_square(board, revealed, *x) + pygame.time.wait(500) + for x in l: + revealed[x[0]][x[1]] = False + draw_square(board, revealed, *x) + + +def get_random_board(shape, colors): + """ Generates the board by random shuffling""" + + icons = list(product(shape, colors)) + num_icons = BOARD_HEIGHT * BOARD_WIDTH // 2 + icons = icons[:num_icons] * 2 + + random.shuffle(icons) + board = [icons[i:i + BOARD_WIDTH] + for i in range(0, BOARD_HEIGHT * BOARD_WIDTH, BOARD_WIDTH)] + return board + + +def get_coord(x, y): + """ Gets the coordinates of particular square. + The squares are number height wise and then width wise. + So the x and y are interchanged.""" + + top = X_MARGIN + y * (SQUARE_SIZE + SQUARE_GAP) + left = Y_MARGIN + x * (SQUARE_SIZE + SQUARE_GAP) + return top, left + + +def draw_icon(icon, x, y): + """Draws the icon of (x, y) square""" + + px, py = get_coord(x, y) + if icon[0] == DIAMOND: + pygame.draw.polygon(screen, icon[1], + ((px + SQUARE_SIZE // 2, py + 5), (px + SQUARE_SIZE - 5, py + SQUARE_SIZE // 2), + (px + SQUARE_SIZE // 2, py + SQUARE_SIZE - 5), (px + 5, py + SQUARE_SIZE // 2))) + elif icon[0] == SQUARE: + pygame.draw.rect(screen, icon[1], + (px + 5, py + 5, SQUARE_SIZE - 10, SQUARE_SIZE - 10)) + elif icon[0] == TRIANGLE: + pygame.draw.polygon(screen, icon[1], + ((px + SQUARE_SIZE // 2, py + 5), (px + 5, py + SQUARE_SIZE - 5), + (px + SQUARE_SIZE - 5, py + SQUARE_SIZE - 5))) + elif icon[0] == CIRCLE: + pygame.draw.circle(screen, icon[1], + (px + SQUARE_SIZE // 2, py + SQUARE_SIZE // 2), SQUARE_SIZE // 2 - 5) + + +def get_pos(cx, cy): + """Gets the square (x, y) position from the cartesian coordinates. + The squares are number height wise and then width wise. + So the cx and cy are interchanged.""" + + if cx < X_MARGIN or cy < Y_MARGIN: + return None, None + + x = (cy - Y_MARGIN) // (SQUARE_SIZE + SQUARE_GAP) + y = (cx - X_MARGIN) // (SQUARE_SIZE + SQUARE_GAP) + + if x >= BOARD_HEIGHT or y >= BOARD_WIDTH or (cx - X_MARGIN) % (SQUARE_SIZE + SQUARE_GAP) > SQUARE_SIZE or (cy - Y_MARGIN) % (SQUARE_SIZE + SQUARE_GAP) > SQUARE_SIZE: + return None, None + else: + return x, y + + +def draw_square(board, revealed, x, y): + """Draws a particular square""" + + coords = get_coord(x, y) + square_rect = (*coords, SQUARE_SIZE, SQUARE_SIZE) + pygame.draw.rect(screen, BGCOLOR, square_rect) + if revealed[x][y]: + draw_icon(board[x][y], x, y) + else: + pygame.draw.rect(screen, Color('gold'), square_rect) + pygame.display.update(square_rect) + + +def draw_board(board, revealed): + """Draws the entire board""" + + for x in range(BOARD_HEIGHT): + for y in range(BOARD_WIDTH): + draw_square(board, revealed, x, y) + + +def draw_select_box(x, y): + """Draws the highlight box around the square""" + + px, py = get_coord(x, y) + pygame.draw.rect(screen, Color('red'), (px - 5, py - 5, SQUARE_SIZE + 10, SQUARE_SIZE + 10), 5) + + +if __name__ == '__main__': + main() \ No newline at end of file