Red Mars

Remember how I said a few hours ago that I was going to wait until the summer to learn PyGame? Well, I lied. I guess that’s what happens when you finish your research paper early and you don’t want to work on your homework for later in the week. It also doesn’t help when your roommate is programming all night and you just want to join in as well. So, I started work on a game that is vastly different that the command-sequence one I had planned:

import pygame, sys, time
from pygame.locals import *

pygame.init()
game_clock = pygame.time.Clock()

WINDOWHEIGHT = 400
WINDOWWIDTH = 400
window = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('RED MARS - Test')

WHITE = (255, 255, 255)
RED = (125, 0, 0)

COLOR = WHITE

player = pygame.Rect(16, 16, 48, 48) # top x top y width height
player_front = pygame.image.load('player.gif')
player_back = pygame.image.load('player_back.gif')
player_left = pygame.image.load('player_left.gif')
player_right = pygame.image.load('player_right.gif')
player_sprite_front = pygame.transform.scale(player_front, (48, 48))
player_sprite_back = pygame.transform.scale(player_back, (48, 48))
player_sprite_right = pygame.transform.scale(player_right, (48, 48))
player_sprite_left = pygame.transform.scale(player_left, (48, 48))

player_image = player_sprite_front

MOVE_LEFT = 0
MOVE_RIGHT = 0
MOVE_UP = 0
MOVE_DOWN = 0

MOVE_SPEED = 4

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

        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                MOVE_RIGHT = False
                MOVE_LEFT = True
                player_image = player_sprite_left
            if event.key == K_RIGHT:
                MOVE_RIGHT = True
                MOVE_LEFT = False
                player_image = player_sprite_right
            if event.key == K_UP:
                MOVE_UP = True
                MOVE_DOWN = False
                player_image = player_sprite_back
            if event.key == K_DOWN:
                MOVE_UP = False
                MOVE_DOWN = True
                player_image = player_sprite_front
            if event.key == ord('q'):
                if COLOR == WHITE:
                    COLOR = RED
                else:
                    COLOR = WHITE
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
        if event.type == KEYUP:
            if event.key == K_LEFT:
                MOVE_LEFT = False
            if event.key == K_RIGHT:
                MOVE_RIGHT = False
            if event.key == K_UP:
                MOVE_UP = False
            if event.key == K_DOWN:
                MOVE_DOWN = False

    window.fill(COLOR)

    if MOVE_DOWN and player.bottom < WINDOWHEIGHT:
        player.top += MOVE_SPEED
    if MOVE_UP and player.top > 0:
        player.top -= MOVE_SPEED
    if MOVE_LEFT and player.left > 0:
        player.left -= MOVE_SPEED
    if MOVE_RIGHT and player.right < WINDOWWIDTH:
        player.right += MOVE_SPEED

    window.blit(player_image, player)

    pygame.display.update()
    game_clock.tick(40)

I drew the sprites (plus some terrain ones which I haven’t implemented yet) before even trying to write any code, partially because I didn’t know basically anything about coding in PyGame a few hours ago. My roommate helped me out with picking some colors and with the helmet, plus encouraged me to keep drawing more and different sprites. I finished my drawings about the same time that he finished his project, and then I set to work on the actual program, after reading today’s MDRS Journalist report to my roommates, of course.

It actually works pretty well. Move with the arrow keys, and press ‘q’ to change the background from white to a dull red and back again. Not much else to do, but it does work. I’m planning on having the gameplay be sort of like a cross between Pokémon and The Legend of Zelda, but right now I don’t know exactly what the gameplay will be. I’m still deciding on whether or not I want to keep the free-roving movement that it has now, or limit it to movement via grid. I’m thinking I’ll leave it as is, but that will most likely be dependent on what the gameplay evolves into.

I see this as a good start to my game, don’t you?

Advertisements

One response to “Red Mars

  1. Pingback: Red Mars – Part 2 « Inside a Calculator