Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

개발자입니다

[유튜브 소놀코딩] 8강.캐릭터 충돌하기 / 9강.점수 만들기 / 10강.게임 종료하기 본문

파이게임/파이썬 게임 만들기 강의(유튜브 소놀코딩)

[유튜브 소놀코딩] 8강.캐릭터 충돌하기 / 9강.점수 만들기 / 10강.게임 종료하기

끈기JK 2023. 8. 31. 21:16

pygame17.py

바나나 충돌 감지

바나나가 원숭이에게 닿으면 방향을 변경한다.

  • 바나나의 객체를 저장한다. 객체의 left에 바나나의 x좌표, 객체의 top에 바나나의 y좌표를 넣는다.
  • 원숭이도 동일하게 한다.
  • colliderect() 를 이용해 충돌을 감지한다. 이때 바나나의 x, y speed 부호를 반대로 바꾼다.
import pygame

pygame.init()

background = pygame.display.set_mode((480, 360))
pygame.display.set_caption("SONOL")

image_bg = pygame.image.load("image/image3.jpg")
image_banana = pygame.image.load("image/image2.png")
image_monkey = pygame.image.load("image/image1.png")

size_bg_width = background.get_size()[0]
size_bg_height = background.get_size()[1]

size_bg_width = background.get_size()[0]
size_bg_height = background.get_size()[1]

size_banana_width = image_banana.get_rect().size[0]
size_banana_height = image_banana.get_rect().size[1]

x_pos_banana = size_bg_width/2 - size_banana_width/2
y_pos_banana = 0

x_speed_banana = 1
y_speed_banana = 1

size_monkey_width = image_monkey.get_rect().size[0]
size_monkey_height = image_monkey.get_rect().size[1]

x_pos_monkey = size_bg_width/2 - size_monkey_width/2
y_pos_monkey = size_bg_height - size_monkey_height

to_x = 0

play = True
while play:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                to_x = 1
            if event.key == pygame.K_LEFT:
                to_x = -1
                
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                to_x = 0
            if event.key == pygame.K_LEFT:
                to_x = 0

    if x_pos_monkey<0:
        x_pos_monkey = 0
    elif x_pos_monkey>size_bg_width-size_monkey_width:
        x_pos_monkey =size_bg_width-size_monkey_width
    else:
        x_pos_monkey += to_x


    x_pos_banana += x_speed_banana
    y_pos_banana += y_speed_banana

    if x_pos_banana <= 0:
        x_speed_banana = -x_speed_banana
        x_pos_banana = 0
    elif x_pos_banana >= size_bg_width-size_banana_width:
        x_speed_banana = -x_speed_banana
        x_pos_banana = size_bg_width-size_banana_width
    
    if y_pos_banana <= 0:
        y_speed_banana = -y_speed_banana
        y_pos_banana = 0
    elif y_pos_banana >= size_bg_height-size_banana_height:
        print('바닥')
        y_speed_banana = -y_speed_banana
        y_pos_banana = size_bg_height-size_banana_height

	# 추가
    rect_banana = image_banana.get_rect()
    rect_banana.left = x_pos_banana
    rect_banana.top = y_pos_banana

    rect_monkey = image_monkey.get_rect()
    rect_monkey.left = x_pos_monkey
    rect_monkey.top = y_pos_monkey

    if rect_monkey.colliderect(rect_banana):
        print('원숭이')
        x_speed_banana = -x_speed_banana
        y_speed_banana = -y_speed_banana
    
    background.blit(image_bg, (0, 0))
    background.blit(image_banana, (x_pos_banana, y_pos_banana))
    background.blit(image_monkey, (x_pos_monkey, y_pos_monkey))
    pygame.display.update()

pygame.quit()

 

 

 

test.py

스코어를 화면에 나타내는 방법이다.

import pygame

pygame.init()

background = pygame.display.set_mode((480, 360))
pygame.display.set_caption("SONOL")

font_test = pygame.font.SysFont(None,30)
point = 10

play = True
while play:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False
        
    text = font_test.render(str(point),True,(255,255,255))
    background.blit(text, (100,100))
    pygame.display.update()

pygame.quit()

 

 

 

pygame18.py

포인트 카운트

원숭이가 바나나를 건들면 포인트가 올라간다.

  • font_point 객체를 생성한다.
  • while 문 맨 아래에 font_point 객체를 render 하고 배경에 그린다.
import pygame

pygame.init()

background = pygame.display.set_mode((480, 360))
pygame.display.set_caption("SONOL")

image_bg = pygame.image.load("image/image3.jpg")
image_banana = pygame.image.load("image/image2.png")
image_monkey = pygame.image.load("image/image1.png")

size_bg_width = background.get_size()[0]
size_bg_height = background.get_size()[1]

size_bg_width = background.get_size()[0]
size_bg_height = background.get_size()[1]

size_banana_width = image_banana.get_rect().size[0]
size_banana_height = image_banana.get_rect().size[1]

x_pos_banana = size_bg_width/2 - size_banana_width/2
y_pos_banana = 0

x_speed_banana = 1
y_speed_banana = 1

size_monkey_width = image_monkey.get_rect().size[0]
size_monkey_height = image_monkey.get_rect().size[1]

x_pos_monkey = size_bg_width/2 - size_monkey_width/2
y_pos_monkey = size_bg_height - size_monkey_height

to_x = 0

# 추가
point = 0
font_point = pygame.font.SysFont(None,30)

play = True
while play:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                to_x = 1
            if event.key == pygame.K_LEFT:
                to_x = -1
                
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                to_x = 0
            if event.key == pygame.K_LEFT:
                to_x = 0

    if x_pos_monkey<0:
        x_pos_monkey = 0
    elif x_pos_monkey>size_bg_width-size_monkey_width:
        x_pos_monkey =size_bg_width-size_monkey_width
    else:
        x_pos_monkey += to_x


    x_pos_banana += x_speed_banana
    y_pos_banana += y_speed_banana

    if x_pos_banana <= 0:
        x_speed_banana = -x_speed_banana
        x_pos_banana = 0
    elif x_pos_banana >= size_bg_width-size_banana_width:
        x_speed_banana = -x_speed_banana
        x_pos_banana = size_bg_width-size_banana_width
    
    if y_pos_banana <= 0:
        y_speed_banana = -y_speed_banana
        y_pos_banana = 0
    elif y_pos_banana >= size_bg_height-size_banana_height:
        print('바닥')
        y_speed_banana = -y_speed_banana
        y_pos_banana = size_bg_height-size_banana_height

    rect_banana = image_banana.get_rect()
    rect_banana.left = x_pos_banana
    rect_banana.top = y_pos_banana

    rect_monkey = image_monkey.get_rect()
    rect_monkey.left = x_pos_monkey
    rect_monkey.top = y_pos_monkey

    if rect_monkey.colliderect(rect_banana):
        print('원숭이')
        x_speed_banana = -x_speed_banana
        y_speed_banana = -y_speed_banana
        point += 1
    
    background.blit(image_bg, (0, 0))
    background.blit(image_banana, (x_pos_banana, y_pos_banana))
    background.blit(image_monkey, (x_pos_monkey, y_pos_monkey))

	# 추가
    text_point = font_point.render(str(point),True,(0,0,0))
    background.blit(text_point, (10, 10))

    pygame.display.update()

pygame.quit()

 

 

 

pygame18.py

바나나가 땅에 닿으면 게임 종료
  • font 객체를 생성한다.
  • banana 의 y 좌표가 바닥에 닿는 if 문에 GAME OVER 를 출력하고 2초 딜레이 준 뒤에 창을 닫는다.
import pygame

pygame.init()

background = pygame.display.set_mode((480, 360))
pygame.display.set_caption("SONOL")

image_bg = pygame.image.load("image/image3.jpg")
image_banana = pygame.image.load("image/image2.png")
image_monkey = pygame.image.load("image/image1.png")

size_bg_width = background.get_size()[0]
size_bg_height = background.get_size()[1]

size_bg_width = background.get_size()[0]
size_bg_height = background.get_size()[1]

size_banana_width = image_banana.get_rect().size[0]
size_banana_height = image_banana.get_rect().size[1]

x_pos_banana = size_bg_width/2 - size_banana_width/2
y_pos_banana = 0

x_speed_banana = 1
y_speed_banana = 1

size_monkey_width = image_monkey.get_rect().size[0]
size_monkey_height = image_monkey.get_rect().size[1]

x_pos_monkey = size_bg_width/2 - size_monkey_width/2
y_pos_monkey = size_bg_height - size_monkey_height

to_x = 0

point = 0
font_point = pygame.font.SysFont(None,30)

# 추가
font_gameover = pygame.font.SysFont(None,80)
text_gameover = font_gameover.render("GAME OVER",True,(255,0,0))

size_text_width = text_gameover.get_rect().size[0]
size_text_height = text_gameover.get_rect().size[1]

x_pos_text = size_bg_width/2-size_text_width/2
y_pos_text = size_bg_height/2-size_text_height/2

play = True
while play:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                to_x = 1
            if event.key == pygame.K_LEFT:
                to_x = -1
                
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                to_x = 0
            if event.key == pygame.K_LEFT:
                to_x = 0

    if x_pos_monkey<0:
        x_pos_monkey = 0
    elif x_pos_monkey>size_bg_width-size_monkey_width:
        x_pos_monkey =size_bg_width-size_monkey_width
    else:
        x_pos_monkey += to_x


    x_pos_banana += x_speed_banana
    y_pos_banana += y_speed_banana

    if x_pos_banana <= 0:
        x_speed_banana = -x_speed_banana
        x_pos_banana = 0
    elif x_pos_banana >= size_bg_width-size_banana_width:
        x_speed_banana = -x_speed_banana
        x_pos_banana = size_bg_width-size_banana_width
    
    if y_pos_banana <= 0:
        y_speed_banana = -y_speed_banana
        y_pos_banana = 0
    elif y_pos_banana >= size_bg_height-size_banana_height:
    	# 추가
        background.blit(text_gameover, (x_pos_text,y_pos_text))
        pygame.display.update()
        pygame.time.delay(2000)
        play = False

    rect_banana = image_banana.get_rect()
    rect_banana.left = x_pos_banana
    rect_banana.top = y_pos_banana

    rect_monkey = image_monkey.get_rect()
    rect_monkey.left = x_pos_monkey
    rect_monkey.top = y_pos_monkey

    if rect_monkey.colliderect(rect_banana):
        x_speed_banana = -x_speed_banana
        y_speed_banana = -y_speed_banana
        point += 1
    
    background.blit(image_bg, (0, 0))
    background.blit(image_banana, (x_pos_banana, y_pos_banana))
    background.blit(image_monkey, (x_pos_monkey, y_pos_monkey))

    text_point = font_point.render(str(point),True,(0,0,0))
    background.blit(text_point, (10, 10))

    pygame.display.update()

pygame.quit()