뜬금 파이썬으로 게임 만들어보기 ep.1

John Jeong·2020년 10월 3일
0
post-thumbnail

개발 동기

요새 총기 유투브를 즐겨보고 있다. (ex. FPSRussia, Kentucky Ballistic, etc.)
그러다 보니까 총기에 대한 관심과 함께 자연스럽게 이러한 궁금증이 생겼다.

한국전쟁이 다시 일어난다면, 용사들이 평균적으로 몇 발 쏘고 전사하게 될까?

매우 암울한 상상이지만, 전쟁이 일어나면 사람이 죽게 될 것이다. 특히 육군 병력 중 용사들의 희생이 엄청나게 될텐데, 전사하기 전까지 평균적으로 몇 발의 총알을 소모하게 되는지 궁금해졌다.

그래서 시뮬레이션을 해보자고 생각했고 다양한 시각화 툴을 찾아보았다. 그렇게 해서 골라진 후보는 파이썬(Python) 모듈 파이게임(pygame)과 유니티(Unity)였다. 안타깝게도 유니티가 훨씬 입체적인 환경을 제공해주지만(애초에 3차원 게임 만드는데 주로 사용되니까), 유니티를 써본 적이 없어서 파이썬을 이용하기로 했다.

그렇게 해서 대략적인 방향성을 생각하고 그에 맞는 정보를 찾기로 했다.

시뮬레이션 가정

  1. 오로지 보병만 존재한다.
  2. 개활지에서 전투한다.
  3. 처음 병력이 스폰되는 위치는 랜덤이다.
  4. 병력은 이동할 수 있다.

시뮬레이션에 필요한 정보

  1. AK-47와 K-2의 긴급 상황에서 또는 거리별 명중률
  2. 파이게임 사용법
  3. 파이게임을 이용한 간단한 게임 만들어보기 ex. Pong
  4. 마우스로 조작되는 것이 아닌 자동으로 게임이 진행하는 방법

그렇게 해서 만들어본 첫 파이게임

아직까지는 미숙한 상태이고 여기서 더 발전시켜서 시뮬레이션까지 가보자!

파이썬 코드

import pygame, sys

# Player character
class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load("./images/soldier1.png")
        self.image = pygame.transform.scale(self.image, (100,80))
        self.rect = self.image.get_rect(center = (screen_width/2, screen_height/2))

    def update(self):
        self.rect.center = pygame.mouse.get_pos()

    def create_bullet(self):
        return Bullet(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1] - 30)

# Bullet object
class Bullet(pygame.sprite.Sprite):
    def __init__(self, pos_x, pos_y):
        super().__init__()
        self.image = pygame.transform.scale(pygame.image.load("./images/556bullet.png"), (10,5))
        self.rect = self.image.get_rect(center = (pos_x, pos_y))

    def update(self):
        self.rect.x += 20

        if self.rect.x >= screen_width:
            self.kill()


# General setup
pygame.init()
clock = pygame.time.Clock()

# Setting up the main window
screen_width, screen_height = 1280, 960
# Display surface -> the main screen where everything is done
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.mouse.set_visible(False)
pygame.display.set_caption('War simulation')

player = Player()
player_group = pygame.sprite.Group()
player_group.add(player)
bullet_group = pygame.sprite.Group()

while True:
    # Handling input
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            bullet_group.add(player.create_bullet())

    # Updating the window
    screen.fill((30,30,30))
    bullet_group.draw(screen)
    player_group.draw(screen)
    player_group.update()
    bullet_group.update()
    pygame.display.flip()
    clock.tick(120)

참고 링크

  1. Learning Pygame by making Pong
  2. Python / Pygame Tutorial: An introduction to sprites
  3. Python / Pygame Tutorial: Creating a basic bullet shooting mechanic

TO BE CONTINUED...

profile
Do or do not, there is no try.

0개의 댓글