파이썬 이론 공부가 지루해서 만들어보는 간단한 게임
참고한 영상
먼저 pygame 이라는 라이브러리 모듈을 설치해줘야 한다
(나는 윈도우 유저이므로 윈도우에 맞춰서 설명하겠다)
윈도우키 + r 을 해서 command 창을 띄워준다
pip install pygame 을 입력해서 pygame 모듈을 불러온다
설치됐는지 확인하려면 파이썬 IDLE 에 import pygame 을 입력
게임을 만들기 위한 초기 세팅 과정!
지금은 하나도 이해하지 못하고 따라쳤다
파이썬을 공부하며 오늘 따라 만든 코드를 해석해 나가겠음
import pygame
import sys
import time
import random
from pygame.locals import *
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
WHITE = (255, 255, 255)
if __name__ == '__main__':
pygame.init()
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)
pygame.display.set_caption('Python Game')
surface = pygame.Surface(window.get_size())
surface = surface.convert()
surface.fill(WHITE)
clock = pygame.time.Clock()
pygame.key_set_repeat(1, 40)
window.blit(surface, (0, 0))
열심히 코드를 따라 쳤지만
뱀이 움직이던 방향으로만 움직이고
키보드 방향키가 동작하지 않는다ㅎㅎ,,,
뭐가 잘못된 걸까........
우선은 선생님의 화면으로 만족하고
파이썬을 더 공부하고 오겠다ㅠㅠ
import pygame
import sys
import time
import random
from pygame.locals import *
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
GRID_SIZE = 20
GRID_WIDTH = WINDOW_WIDTH / GRID_SIZE
GRID_HEIGHT = WINDOW_HEIGHT / GRID_SIZE
WHITE = (255, 255, 255)
GREEN = (0, 50, 0)
ORANGE = (250, 150, 0)
GRAY = (100, 100, 100)
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)
FPS = 10
class Python(object):
def __init__(self):
self.create()
self.color = GREEN
def create(self):
self.length = 2
self.positions = [((WINDOW_WIDTH / 2), (WINDOW_HEIGHT / 2))]
self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
def control(self, xy):
if (xy[0] * -1, xy[1] * -1) == self.direction:
return
else:
self.direction == xy
def move(self):
cur = self.positions[0]
x, y = self.direction
new = (((cur[0] + (x * GRID_SIZE)) % WINDOW_WIDTH), (cur[1] + (y * GRID_SIZE)) % WINDOW_HEIGHT)
if new in self.positions[2:]:
self.create()
else:
self.positions.insert(0, new)
if len(self.positions) > self.length:
self.positions.pop()
def eat(self):
self.length += 1
def draw(self, surface):
for p in self.positions:
draw_object(surface, self.color, p)
class Feed(object):
def __init__(self):
self.position = (0, 0)
self.color = ORANGE
self.create()
def create(self):
self.position = (random.randint(0, GRID_WIDTH - 1) * GRID_SIZE, random.randint(0, GRID_HEIGHT - 1) * GRID_SIZE)
def draw(self, surface):
draw_object(surface, self.color, self.position)
def draw_object(surface, color, pos):
r = pygame.Rect((pos[0], pos[1]), (GRID_SIZE, GRID_SIZE))
pygame.draw.rect(surface, color, r)
def check_eat(python, feed):
if python.positions[0] == feed.position:
python.eat()
feed.create()
def show_info(length, speed, surface):
font = pygame.font.Font(None, 34)
text = font.render("Length: " + str(length) + " Speed: " + str(round(speed, 2)), 1, GRAY)
pos = text.get_rect()
pos.centerx = 150
surface.blit(text, pos)
if __name__ == '__main__':
python = Python()
feed = Feed()
pygame.init()
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)
pygame.display.set_caption('Python Game')
surface = pygame.Surface(window.get_size())
surface = surface.convert()
surface.fill(WHITE)
clock = pygame.time.Clock()
pygame.key.set_repeat(1, 40)
window.blit(surface, (0, 0))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_UP:
python.control(UP)
elif event.key == K_DOWN:
python.control(DOWN)
elif event.key == K_LEFT:
python.control(LEFT)
elif event.key == K_RIGHT:
python.control(RIGHT)
surface.fill(WHITE)
python.move()
check_eat(python, feed)
speed = (FPS + python.length) / 2
show_info(python.length, speed, surface)
python.draw(surface)
feed.draw(surface)
window.blit(surface, (0, 0))
pygame.display.flip()
pygame.display.update()
clock.tick(speed)