Monthly Archives: April 2010

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

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?

The End of the Semester

Starting this weekend, I am in finals mode. I didn’t spend enough time studying last semester, and it showed slightly in my grades, so this time I’m making a more focused and coherent effort to prepare for my end of year exams. One good thing about this semester is that I only have three finals; my HPS class will just have a final three-page paper (I just finished the long paper, which is due tomorrow and takes up more of my grade), while my Electronics class has half of the grade weighted into the lab portion. Yes, I’ll still have quite a bit of work to do for all five of my courses even while studying, but at least my time actually taking exams will be minimal.

I scheduled my courses for next year, with only a few small alterations from what I said a month ago. For instance, I’ll be taking MTH 451 [Numerical Analysis I] in the fall, while my spring Math courses are MTH 415 [Applied Linear Algebra] and MTH 442 [Partial Differential Equations]. There also was a little shuffling of my Physics courses due to when they were actually offered, but that’s fine by me. I’m only taking fourteen credits each semester, plus have no lab in the fall, so I’m happy. That might change when I wake up for 0910 classes every morning, but since I got so lucky this year with my times, I knew that it would eventually run out.

My summer plans are getting closer to being set. I applied for two jobs last weekend and am still waiting to hear back from, I’m on the counselor list for MST@MSU 2010, I am all set for my drive to Florida to see the STS-132 launch, and I’m living in my apartment with both Greg and Chris. I don’t think it could get any better than that. Technically, I’m still waiting to hear back about the NASA USRP, but I’m not very optimistic about it… Oh well, since I’ve had some great research experience so far anyway that working for NASA would’ve just been icing and not the whole cake. Speaking of research, I’ll be presenting at the 2010 UURAF this Friday, with only a few small modifications to do to my poster before then. Plus, to celebrate, I have a Calculus LA pub crawl that night…

I’m also looking forward to having some free time. I’ve been playing two online games pretty frequently recently: Light Bot and NASA ROVER. Both are command-sequence run games where you set a command sequence, hit run, and the robot/rover follows that sequence to achieve a goal, whether it’s moving to a certain point or switching on lights. I’m planning on writing a similar game in Python (using PyGame) this summer, which should be a good couple months of programming and testing. Plus, I’m just looking forward to playing it and expanding it as I go along. I’ve worked on some character art already, plus planning out how I want it to function, so I really just need to learn the ins-and-outs of PyGame before I can get started.

Of course, that won’t be until after the GRE, which I’m taking at the end of May. I’ve been studying vocab like it’s my job, almost to the complete exclusion of every other part of the test. I’m not worried about the math section; I’ll just need to make sure that I don’t make silly mistakes with my arithmetic or the directions. I’ve skimmed through everything, but the vocab is just what I’m worried about. I won’t be taking the Physics subject test until the fall, so I won’t need to worry about that for some time yet.

And that’s basically my life right now. This saturday, I’ll be shutting myself off from online social networking to fully get into finals mode, so you probably won’t hear from me until May 5th or later. So, until then, stay thirsty my friends.