PyGame 스페이스 인베이더(Space Invader) - 좌우로 움직이는 적(Enemy)

|

PyGame 스페이스 인베이더(Space Invader) - 좌우로 움직이는 적(Enemy)

아래는 사용한 이미지입니다. 구글링으로 찾은 무료 이미지이며 크기를 좀 줄였습니다. (40x40 사이즈입니다.)

image


소스 코드

적은 좌우로 움직이며, 양쪽 끝에 도달할 경우 아래로 이동합니다.

import pygame
import sys

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

white = (255, 255, 255)
black = (0, 0, 0)

pygame.init()
pygame.display.set_caption("Space Invaders")
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()


class Enemy(object):
    def __init__(self):
        self.image = pygame.image.load("images/space_invader_enemy.png").convert_alpha()
        self.x = 100
        self.y = 20
        self.speed = 3
        self.direction = 1

    def move(self):
        self.x += self.speed * self.direction

        if self.x <= 0:
            self.y += 20
            self.direction = 1

        if self.x + 40 >= SCREEN_WIDTH:
            self.y += 20
            self.direction = -1

    def draw(self):
        screen.blit(self.image, (self.x, self.y))


def main():
    enemy = Enemy()

    while True:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        screen.fill(white)

        enemy.move()
        enemy.draw()

        pygame.display.update()


if __name__ == "__main__":
    main()

PyGame 빗방울과 캐릭터간 충돌 체크하기

|

PyGame 빗방울과 캐릭터간 충돌 체크하기

import pygame
import sys
import random

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

white = (255, 255, 255)
black = (0, 0, 0)

pygame.init()
pygame.display.set_caption("Rain")
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()


class Raindrop(object):
    def __init__(self):
        self.height = random.randint(4, 7)
        self.speed = random.randint(5, 10)
        self.x = random.randint(0, SCREEN_WIDTH)
        self.y = -self.height

    def move(self):
        self.y += self.speed

    def draw(self):
        pygame.draw.line(screen, white, (self.x, self.y), (self.x, self.y + self.height), 1)


class Player(object):
    def __init__(self):
        self.x = 100
        self.y = 300
        self.image = pygame.image.load("images/player.png").convert_alpha()

    def draw(self):
        screen.blit(self.image, (self.x, self.y))

    def is_collide(self, x, y):
        return pygame.Rect(self.x, self.y, 65, 120).collidepoint(x, y)


def main():
    raindrops = []
    player = Player()

    while True:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        screen.fill(black)
        raindrops.append(Raindrop())

        player.draw()
        for drop in raindrops:
            drop.move()
            drop.draw()

            if drop.y > SCREEN_HEIGHT or player.is_collide(drop.x, drop.y):
                raindrops.remove(drop)

        pygame.display.update()


if __name__ == "__main__":
    main()


키보드 이벤트로 캐릭터 이동시키기

import pygame
import sys
import random

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

white = (255, 255, 255)
black = (0, 0, 0)

pygame.init()
pygame.display.set_caption("Rain")
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()


class Raindrop:
    def __init__(self):
        self.height = random.randint(4, 7)
        self.speed = random.randint(5, 10)
        self.x = random.randint(0, SCREEN_WIDTH)
        self.y = -self.height

    def move(self):
        self.y += self.speed

    def draw(self):
        pygame.draw.line(screen, white, (self.x, self.y), (self.x, self.y + self.height), 1)


class Player:
    def __init__(self):
        self.x = 100
        self.y = 300
        self.image = pygame.image.load("images/player.png").convert_alpha()

    def draw(self):
        screen.blit(self.image, (self.x, self.y))

    def is_collide(self, x, y):
        return pygame.Rect(self.x, self.y, 65, 120).collidepoint(x, y)


def main():
    raindrops = []
    player = Player()

    while True:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        screen.fill(black)
        raindrops.append(Raindrop())

        player.draw()
        for drop in raindrops:
            drop.move()
            drop.draw()

            if drop.y > SCREEN_HEIGHT or player.is_collide(drop.x, drop.y):
                raindrops.remove(drop)

        pygame.display.update()


if __name__ == "__main__":
    main()

PyGame 기반 비오는 장면 속의 캐릭터 연출

|

PyGame 기반 비오는 장면 속의 캐릭터 연출

다음과 같은 화면을 만들어보는 예제입니다.

image


아래는 캐릭터에 사용한 이미지입니다. 구글링으로 찾은 무료 이미지이며 크기를 좀 줄였습니다. (65x120 사이즈입니다.)

image


소스 코드

import pygame
import sys

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

white = (255, 255, 255)
black = (0, 0, 0)

pygame.init()
pygame.display.set_caption("Rain")
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()


class Player(object):
    def __init__(self):
        self.x = 100
        self.y = 300
        self.image = pygame.image.load("images/player.png").convert_alpha()

    def draw(self):
        screen.blit(self.image, (self.x, self.y))


def main():
    player = Player()

    while True:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        screen.fill(black)
        player.draw()

        pygame.display.update()


if __name__ == "__main__":
    main()

PyGame 기반 PNG 이미지 로드하는 방법

|

PyGame 기반 PNG 이미지 로드하는 방법

import pygame
import sys

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

white = (255, 255, 255)
black = (0, 0, 0)

pygame.init()
pygame.display.set_caption("Rain")
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()


class Player(object):
    def __init__(self):
        self.x = 100
        self.y = 300
        self.image = pygame.image.load("images/player.png").convert_alpha()

    def draw(self):
        screen.blit(self.image, (self.x, self.y))


def main():
    player = Player()

    while True:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        screen.fill(black)
        player.draw()

        pygame.display.update()


if __name__ == "__main__":
    main()

PyGame 기반 비오는 장면 연출

|

PyGame 기반 비오는 장면 연출

import pygame
import sys
import random

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

white = (255, 255, 255)
black = (0, 0, 0)

pygame.init()
pygame.display.set_caption("Rain")
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()


class Raindrop(object):
    def __init__(self):
        self.height = random.randint(4, 7)
        self.speed = random.randint(5, 10)
        self.x = random.randint(0, SCREEN_WIDTH)
        self.y = -self.height

    def move(self):
        self.y += self.speed

    def draw(self):
        pygame.draw.line(screen, white, (self.x, self.y), (self.x, self.y + self.height), 1)


def main():
    raindrops = []

    while True:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        raindrops.append(Raindrop())
        screen.fill(black)

        for drop in raindrops:
            drop.move()
            drop.draw()

            if drop.y > SCREEN_HEIGHT:
                raindrops.remove(drop)

        pygame.display.update()


if __name__ == "__main__":
    main()