Red Mars – Part 2

This weekend, following the ‘Related Rates Festival’ on Friday and concluding with homework tonight, was pretty productive… At least, productive for my game. I didn’t clean my room, do much work at all, read a book, or anything, but I did break a lot of ground on my game. I even recorded a short video of me moving around the character (below)! Now, the video doesn’t show all of the character sprites, or even all of the terrain sprites, since it was more just a “make sure all that coding I did actually works” project.

I’ve updated the terrain sprites tremendously since the last update, and they look much better. I’m still, however, not completely sold on the cliff edges, especially the ones that appear prominently on the lower screen. I’ll probably fuss with all of the images some more, but for now I’m getting back to actually coding it, so the pretty pictures will have to wait.

Plus, I will almost definitely be changing up the graphics once everything is set, since using GameBoy-esque graphics on a MacBook just seems wrong (but also quite nostalgic… so they may stay…). Still no plan for story, tasks, map, etc., but I’m getting closer to the point where I can just focus on those things.

And for those interested…:

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

pygame.mixer.pre_init(32000)
pygame.init()
game_clock = pygame.time.Clock()

WINDOWWIDTH = 720
WINDOWHEIGHT = 480
COLORKEY = (0, 255, 0, 0)
window = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('RED MARS - Directory Change Test')

def create_sprite(name, scalex, scaley):
    file = pygame.image.load(name)
    file = file.convert()
    file.set_colorkey(COLORKEY, RLEACCEL)
    file = pygame.transform.scale(file, (scalex, scaley))
    return file

def rock_fill():
    rocks = []
    for j in range(10):
        for i in range(15):
            if screen[j][i] == 0:
                pass
            elif screen[j][i] == 1 or screen[j][i] == 2 or screen[j][i] == 3 or screen[j][i] == 9 or screen[j][i] == 10 or screen[j][i] == 15:
                rocks.append(pygame.Rect(i*48+2, j*48+2, 44, 44))
            elif screen[j][i] == 4 or screen[j][i] == 11:
                rocks.append(pygame.Rect(i*48+2, j*48+2, 17, 44))
            elif screen[j][i] == 5 or screen[j][i] == 12:
                rocks.append(pygame.Rect(i*48+29, j*48+2, 17, 44))
            elif screen[j][i] == 6:
                rocks.append(pygame.Rect(i*48+2, j*48+2, 17, 44))
                rocks.append(pygame.Rect(i*48+23, j*48+2, 23, 17))
            elif screen[j][i] == 7:
                rocks.append(pygame.Rect(i*48+2, j*48+2, 44, 17))
            elif screen[j][i] == 8:
                rocks.append(pygame.Rect(i*48+2, j*48+2, 23, 17))
                rocks.append(pygame.Rect(i*48+29, j*48+2, 17, 44))
            elif screen[j][i] == 13:
                rocks.append(pygame.Rect(i*48+2, j*48+2, 17, 17))
            elif screen[j][i] == 14:
                rocks.append(pygame.Rect(i*48+29, j*48+2, 17, 17))
            else:
                pass
    return rocks

def screen_blit():
    for j in range(10):
        for i in range(15):
            if screen[j][i] == 0:
                image = sand
            elif screen[j][i] == 1:
                image = cliff_1
            elif screen[j][i] == 2:
                image = cliff_2
            elif screen[j][i] == 3:
                image = cliff_3
            elif screen[j][i] == 4:
                image = cliff_4
            elif screen[j][i] == 5:
                image = cliff_5
            elif screen[j][i] == 6:
                image = cliff_6
            elif screen[j][i] == 7:
                image = cliff_7
            elif screen[j][i] == 8:
                image = cliff_8
            elif screen[j][i] == 9:
                image = cliff_9
            elif screen[j][i] == 10:
                image = cliff_10
            elif screen[j][i] == 11:
                image = cliff_11
            elif screen[j][i] == 12:
                image = cliff_12
            elif screen[j][i] == 13:
                image = cliff_13
            elif screen[j][i] == 14:
                image = cliff_14
            elif screen[j][i] == 15:
                image = rock_1
            else:
                image = sand
            window.blit(image, pygame.Rect(i*48, j*48, 48, 48))

def point_inside(x, y, rect):
    if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom):
        return True
    else:
        return False

def collision_detect(rect1, rect2):
    for a, b in [(rect1, rect2), (rect2, rect1)]:
        if ((point_inside(a.left, a.top, b)) or (point_inside(a.left, a.bottom, b)) or (point_inside(a.right, a.top, b)) or (point_inside(a.right, a.bottom, b))):
            return True
    return False

cliff_1 = create_sprite('./red_mars/Terrain_Sprites/cliff_1.gif', 48, 48)
cliff_2 = create_sprite('./red_mars/Terrain_Sprites/cliff_2.gif', 48, 48)
cliff_3 = pygame.transform.flip(cliff_1, True, False)
cliff_4 = create_sprite('./red_mars/Terrain_Sprites/cliff_6.gif', 48, 48)
cliff_5 = pygame.transform.flip(cliff_4, True, False)
cliff_6 = create_sprite('./red_mars/Terrain_Sprites/cliff_9.gif', 48, 48)
cliff_7 = create_sprite('./red_mars/Terrain_Sprites/cliff_5.gif', 48, 48)
cliff_8 = pygame.transform.flip(cliff_6, True, False)
cliff_9 = create_sprite('./red_mars/Terrain_Sprites/cliff_3.gif', 48, 48)
cliff_10 = pygame.transform.flip(cliff_9, True, False)
cliff_11 = create_sprite('./red_mars/Terrain_Sprites/cliff_4.gif', 48, 48)
cliff_12 = pygame.transform.flip(cliff_11, True, False)
cliff_13 = create_sprite('./red_mars/Terrain_Sprites/cliff_7.gif', 48, 48)
cliff_14 = pygame.transform.flip(cliff_13, True, False)
rock_1 = create_sprite('./red_mars/Terrain_Sprites/rock_1.gif', 48, 48)
sand = create_sprite('./red_mars/Terrain_Sprites/sand_ml_1.gif', 48, 48)

screen_1 = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 12, 2, 2, 2, 2, 2, 11, 0, 0, 0, 0, 0], [0, 0, 0, 5, 2, 2, 2, 2, 2, 4, 0, 0, 0, 0, 0], [0, 0, 0, 5, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0], [0, 0, 0, 5, 0, 0, 0, 0, 0, 1, 2, 2, 2, 2, 2], [12, 2, 2, 3, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 15], [5, 2, 2, 3, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 15], [5, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 15], [5, 0, 0, 0, 0, 6, 7, 7, 7, 13, 0, 0, 0, 0, 15], [5, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 15], [5, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 15]]
screen_2 = [[5, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 15], [5, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 15], [5, 0, 0, 0, 0, 1, 10, 0, 0, 9, 2, 2, 2, 2, 2], [5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15], [5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15], [14, 7, 7, 7, 7, 7, 7, 7, 8, 0, 0, 0, 0, 0, 15], [0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 15], [0, 0, 0, 0, 0, 0, 0, 0, 14, 7, 7, 7, 7, 7, 7], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

screen = screen_1
rocks = rock_fill()

player = pygame.Rect(288, 288, 48, 48)
sprite_back = create_sprite('./red_mars/Character_Sprites/back_surface.gif', 48, 48)
sprite_back_w = create_sprite('./red_mars/Character_Sprites/back_surface_w.gif', 48, 48)
sprite_back_w2 = create_sprite('./red_mars/Character_Sprites/back_surface_w2.gif', 48, 48)
sprite_front = create_sprite('./red_mars/Character_Sprites/front_surface.gif', 48, 48)
sprite_front_w = create_sprite('./red_mars/Character_Sprites/front_surface_w.gif', 48, 48)
sprite_front_w2 = create_sprite('./red_mars/Character_Sprites/front_surface_w2.gif', 48, 48)
sprite_left = create_sprite('./red_mars/Character_Sprites/left_surface.gif', 48, 48)
sprite_left_w = create_sprite('./red_mars/Character_Sprites/left_surface_w.gif', 48, 48)
sprite_right = pygame.transform.flip(sprite_left, True, False)
sprite_right_w = pygame.transform.flip(sprite_left_w, True, False)

player_image = sprite_front

MOVE_LEFT = False
MOVE_RIGHT = False
MOVE_UP = False
MOVE_DOWN = False
MOVE_SPEED = 4

Walk_Counter = 0

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_LEFT = True
                MOVE_RIGHT = False
                MOVE_UP = False
                MOVE_DOWN = False
                player_image = sprite_left
            if event.key == K_RIGHT:
                MOVE_LEFT = False
                MOVE_RIGHT = True
                MOVE_UP = False
                MOVE_DOWN = False
                player_image = sprite_right
            if event.key == K_UP:
                MOVE_LEFT = False
                MOVE_RIGHT = False
                MOVE_UP = True
                MOVE_DOWN = False
                player_image = sprite_back
            if event.key == K_DOWN:
                MOVE_LEFT = False
                MOVE_RIGHT = False
                MOVE_UP = False
                MOVE_DOWN = True
                player_image = sprite_front
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
        if event.type == KEYUP:
            if event.key == K_LEFT:
                MOVE_LEFT = False
                Walk_Counter = 0
                player_image = sprite_left
            if event.key == K_RIGHT:
                MOVE_RIGHT = False
                Walk_Counter = 0
                player_image = sprite_right
            if event.key == K_UP:
                MOVE_UP = False
                Walk_Counter = 0
                player_image = sprite_back
            if event.key == K_DOWN:
                MOVE_DOWN = False
                Walk_Counter = 0
                player_image = sprite_front

    if MOVE_DOWN and player.bottom < WINDOWHEIGHT:
        player.top += MOVE_SPEED
        for rock in rocks:
            if collision_detect(player, rock):
                player.bottom = rock.top - 2
    if MOVE_UP and player.top > 0:
        player.top -= MOVE_SPEED
        for rock in rocks:
            if collision_detect(player, rock):
                player.top = rock.bottom + 2
    if MOVE_LEFT and player.left > 0:
        player.left -= MOVE_SPEED
        for rock in rocks:
            if collision_detect(player, rock):
                player.left = rock.right + 2
    if MOVE_RIGHT and player.right < WINDOWWIDTH:
        player.right += MOVE_SPEED
        for rock in rocks:
            if collision_detect(player, rock):
                player.right = rock.left - 2

    if MOVE_DOWN and player.bottom >= WINDOWHEIGHT:
        screen = screen_2
        rocks = rock_fill()
        player.top = 1
    if MOVE_UP and player.top <= 0:
        screen = screen_1
        rocks = rock_fill()
        player.bottom = WINDOWHEIGHT - 1

    walking = Walk_Counter % 20
    if MOVE_UP:
        if walking in range(0, 5, 1):
            player_image = sprite_back_w
        elif walking in range(10, 15, 1):
            player_image = sprite_back_w2
        else:
            player_image = sprite_back
        Walk_Counter += 1
    if MOVE_DOWN:
        if walking in range(0, 5, 1):
            player_image = sprite_front_w
        elif walking in range(10, 15, 1):
            player_image = sprite_front_w2
        else:
            player_image = sprite_front
        Walk_Counter += 1
    if MOVE_LEFT:
        if walking in range(0, 5, 1) or walking in range(10, 15, 1):
            player_image = sprite_left_w
        else:
            player_image = sprite_left
        Walk_Counter += 1
    if MOVE_RIGHT:
        if walking in range(0, 5, 1) or walking in range(10, 15, 1):
            player_image = sprite_right_w
        else:
            player_image = sprite_right
        Walk_Counter += 1

    screen_blit()
    window.blit(player_image, player)

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

2 responses to “Red Mars – Part 2

  1. Your game already looks super-cute. Keep it up! 🙂

    • I’ll need to remember to take a vid of the newest incarnation, once I get some of the code sorted out… Terminal seems to not like importing user-made modules.