반응형
SMALL

Pygame 은 비디오 게임 작성 용으로 설계된 크로스 플랫폼 Python 모듈 세트입니다

여기에는 Python 프로그래밍 언어와 함께 사용하도록

설계된 컴퓨터 그래픽 및 사운드 라이브러리가 포함됩니다.

 

Command 열기

[시작] - [실행] - cmd

 

pygame 라이브러리 추가 명령어

pip install pygame

 

Python Shell에서 설치 확인하기

 

import pygame
import sys
import time
import random  

from pygame.locals import *
from operator import pos

WINDOW_WIDTH=800
WINDOW_HEIGTH=600
GRID_SIZE = 20
GRID_WIDTH = WINDOW_WIDTH / GRID_SIZE
GRID_HEIGTH = WINDOW_HEIGTH / GRID_SIZE


WHITE = (255, 255, 255)
GREEN = (0, 50, 0)
ORANGE = (250, 150, 0)

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_HEIGTH / 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_HEIGTH)
        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.positions = (0, 0)
       self.color = ORANGE
       self.create()

    def create(self):
        self.positions = ( random.randint(0, GRID_WIDTH - 1 )* GRID_SIZE, random.randint(0, GRID_HEIGTH -1)* GRID_SIZE)

    def draw(self, surface):
        draw_object(surface, self.color, self.positions)

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.positions:
        python.eat()
        feed.create()

if __name__ == '__main__':
    python = Python()
    feed = Feed()


    pygame.init()
    window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGTH), 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
        python.draw(surface)
        feed.draw(surface)
        window.blit(surface, (0, 0))
        pygame.display.flip()
        pygame.display.update()
        clock.tick(speed)

한번 따라 쳐보세요 ㅎㅎ

혹시 안되면 밑에 소스파일 첨부 합니다.

 

pygame1.py
0.00MB

 

반응형
LIST

+ Recent posts