Wednesday 9 December 2015

Pygame: draw army of soldiers


Following program use ellipse and line to draw an army of soldiers.
# import pygame
import pygame

# initialize game engine
pygame.init()

# Constants related to window
window_width=500
window_height=500
clock_tick_rate=20

# Constants related to soldiers
soldier_head_width=10
soldier_head_height=10
soldier_start_x_position_incr=5

def draw_soldier(soldier_start_x_position, soldier_start_y_position):
    # Soldier head
    pygame.draw.ellipse(screen, BLACK, [soldier_start_x_position+soldier_start_x_position_incr,soldier_start_y_position,soldier_head_width,soldier_head_height], 0)

    # Soldier Body
    pygame.draw.line(screen, RED, [10+soldier_start_x_position, 9+soldier_start_y_position], [10+soldier_start_x_position, 20+soldier_start_y_position], 3)

    # Soldier hands
    pygame.draw.line(screen, RED, [10+soldier_start_x_position, 9+soldier_start_y_position], [5+soldier_start_x_position, 15+soldier_start_y_position], 3)
    pygame.draw.line(screen, RED, [10+soldier_start_x_position, 9+soldier_start_y_position], [15+soldier_start_x_position, 15+soldier_start_y_position], 3)

    #Soldier legs
    pygame.draw.line(screen, BLACK, [10+soldier_start_x_position, 20+soldier_start_y_position], [5+soldier_start_x_position, 26+soldier_start_y_position], 3)
    pygame.draw.line(screen, BLACK, [10+soldier_start_x_position, 20+soldier_start_y_position], [15+soldier_start_x_position, 26+soldier_start_y_position], 3)

# Open a window
size = (window_width, window_height)
screen = pygame.display.set_mode(size)

# Set title to the window
pygame.display.set_caption("Hello World")

dead=False

# Initialize values for color (RGB format)
WHITE=(255,255,255)
RED=(255,0,0)
GREEN=(0,255,0)
BLUE=(0,0,255)
BLACK=(0,0,0)

clock = pygame.time.Clock()

while dead==False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            dead = True

    screen.fill(WHITE)

    for i in range(0, 490, 20):
        for j in range(0, 480, 30):
            draw_soldier(i, j)

    font = pygame.font.SysFont('Calibri', 20, True, True)
    text = font.render("My Army",True,BLACK)
    screen.blit(text, [210, 475])

    pygame.display.flip()
    clock.tick(clock_tick_rate)

Output





Previous                                                 Next                                                 Home

No comments:

Post a Comment