Computer Science Canada

A Namesless Game - Project Progress Thread

Author:  Nathan4102 [ Sat Aug 10, 2013 3:24 pm ]
Post subject:  A Namesless Game - Project Progress Thread

These last few weeks, I've been putting together plans for a pretty big project. In this thread, I'll be posting my code that I've got so far, and ideas/plans for the game. The reason I'm making this thread isn't to get you guys to help me make this, and get you guys to do the hard parts. I'm doing it to get some feedback on code I've written. Please don't post and say "Oh you're trying to do this? Here's the code." That's not what I want! :p

Now, I guess I should explain a bit about this project. The game is going to be kind of like Sims, except A LOT simpler, with less features and stuff. Basically, you have a character, and you need to do stuff with your character to make your life better. Better life = better score. The ways to improve your life are different depending on your motivation chosen at the start of the game. Different stats can be raised and lowered by doing (or not doing) activities. I might also add an achievement type system into this too. For a more in-depth explanation, I've attached my game blueprint to this post in a .doc file.

Progress on this project won't be as fast as some of you might be expecting. I currently work a full time job, and I have other things to get done too. Because of this, I only really get to work on my game 30-60 minutes a day. This thread won't be updated every day, but rather whenever I feel I've done a significant amount of work on the project.

Now, onto the code. This is my current code for the project, as of 10/8/13.

Turing:
%Project - Name undecided
%Programming started July 29, 2013
%More information found in "Game Blueprint.doc"
%Project.t

var llength, height : int %Length and height of our map
var stream : int %Used in extracting map from file
var text : flexible array 1 .. 1 of string %Holds the map
var pic : array 1 .. 21 of int %Array holding all our images
const Char : array 1 .. 21 of string := init ("R", "G", "B", "V", "H", "1", "2", "3", "4", "up", "right", "down", "left", "q", "w", "e", "r", "a", "s", "d", "f") %Different things that could be in the map.txt file
const obstructions : array 1 .. 15 of string := init ("B", "V", "H", "1", "2", "3", "4", "a", "s", "d", "f", "q", "w", "e", "r") %Can't walk through these things
var charx, chary : int %Character position
var chars : array char of boolean %Used for keyboard input
var prevchars : array char of boolean %Used for keyboard input
var facing : string := "up" %Direction character is facing

setscreen ("graphics:600;600, offscreenonly") % Set screensize

pic (1) := Pic.FileNew ("Images/Road.bmp") %Load all the pictures into array
pic (2) := Pic.FileNew ("Images/Grass.bmp")
pic (3) := Pic.FileNew ("Images/Bush.bmp")
pic (4) := Pic.FileNew ("Images/FenceV.bmp")
pic (5) := Pic.FileNew ("Images/FenceH.bmp")
pic (6) := Pic.FileNew ("Images/Fence1.bmp")
pic (7) := Pic.FileNew ("Images/Fence2.bmp")
pic (8) := Pic.FileNew ("Images/Fence3.bmp")
pic (9) := Pic.FileNew ("Images/Fence4.bmp")
pic (10) := Pic.FileNew ("Images/Char.bmp")
pic (11) := Pic.FileNew ("Images/Char1.bmp")
pic (12) := Pic.FileNew ("Images/Char2.bmp")
pic (13) := Pic.FileNew ("Images/Char3.bmp")
pic (14) := Pic.FileNew ("Images/S1.bmp")
pic (15) := Pic.FileNew ("Images/S2.bmp")
pic (16) := Pic.FileNew ("Images/S3.bmp")
pic (17) := Pic.FileNew ("Images/S4.bmp")
pic (18) := Pic.FileNew ("Images/S5.bmp")
pic (19) := Pic.FileNew ("Images/S6.bmp")
pic (20) := Pic.FileNew ("Images/S7.bmp")
pic (21) := Pic.FileNew ("Images/S8.bmp")

open : stream, "Map.txt", get % Open map file and load it into string array

loop
    exit when eof (stream)
    get : stream, text (upper (text))
    new text, upper (text) + 1
end loop

llength := length (text (1)) %Determine length/height of map
height := upper (text) - 1

charx := 15 %Characters starting position
chary := 0

function collision (x, y : int) : boolean %Function checks for collision when walking.
    for i : 1 .. upper (obstructions)
        if text (x) (y) = obstructions (i) then
            result true
        end if
    end for
    result false
end collision

function indexArray (s : string) : int %Function finds index of an element in the char array
    for i : 1 .. 21
        if Char (i) = s then
            result i
        end if
    end for
    result 0
end indexArray

loop
    Input.KeyDown (chars) %Handle keyboard input, Walking stuff below

    if chars (KEY_UP_ARROW) and chars (KEY_UP_ARROW) not= prevchars (KEY_UP_ARROW) then
        facing := "up"
        if collision (15 - chary - 1, charx + 1) = false then
            chary += 1
        end if
    end if

    if chars (KEY_DOWN_ARROW) and chars (KEY_DOWN_ARROW) not= prevchars (KEY_DOWN_ARROW) then
        facing := "down"
        if collision (15 - chary + 1, charx + 1) = false then
            chary -= 1
        end if
    end if

    if chars (KEY_LEFT_ARROW) and chars (KEY_LEFT_ARROW) not= prevchars (KEY_LEFT_ARROW) then
        facing := "left"
        if collision (15 - chary, charx) = false then
            charx -= 1
        end if
    end if

    if chars (KEY_RIGHT_ARROW) and chars (KEY_RIGHT_ARROW) not= prevchars (KEY_RIGHT_ARROW) then
        facing := "right"
        if collision (15 - chary, charx + 2) = false then
            charx += 1
        end if
    end if

    prevchars := chars
   
    if facing not= "" then  %This if statement means the map is only redrawn when we move. Saves CPU
        for i : 1 .. llength
            for ii : 1 .. height
                Pic.Draw (pic (indexArray (text (ii) (i))), i * 40 + 240 - (40 * charx), height * 40 - (ii * 40) - (40 * chary) + 280 - ((height - 15) * 40), picMerge)
            end for
        end for
       
        Pic.Draw (pic (indexArray (facing)), 280, 280, picMerge) %Draw the character
        View.Update
        cls
    end if
   
    facing := ""
end loop


I'll add more onto this thread shortly with stuff like what I'm working on right now, and what my next steps are.

I've also atatched a .rar containing all the images and map files needed for running the game.

Feel free to give me suggestions, and share your thoughts! Smile

Thanks,
Nathan

Author:  Insectoid [ Sat Aug 10, 2013 9:22 pm ]
Post subject:  RE:A Namesless Game - Project Progress Thread

I haven't spent much time looking through it, but at first glance I can tell you can do a much cleaner job of loading images. You're already using an array, so you might as well use a for loop to load them all. Since your images don't share similar names (as a whole, anyway), the easiest method would be to include a text file with all the file names, which the program reads before loading the corresponding image. This will allow you to easily add more images to create more diverse maps.

To expand on that, you can include the character-image relations in the map file at the start, which allows you to use a ton of images for a massive variety of maps without exceeding the 255-character limit of 8-bit ASCII.

Author:  Nathan4102 [ Sat Aug 10, 2013 9:36 pm ]
Post subject:  RE:A Namesless Game - Project Progress Thread

Thanks for the comments! I like your first point, it would be a good way to cut down A LOT of lines of code, especially as my program grows, and I add more images. What do you mean by including character-image relations in the map file? I was aware of the 255 image limit, but I was kind of hoping I wouldn't get to that many images! Razz

Author:  Insectoid [ Sun Aug 11, 2013 4:52 am ]
Post subject:  RE:A Namesless Game - Project Progress Thread

If you have twenty different maps, all with different sprite sets (for different themes), you can easily run over the limit. To get around this, you assign each image to a character in the map files instead of in the code. For example, a map could look something like this:

code:

'#' "grass.bmp"
'^' "wall.bmp"
'@' "water.bmp"
###########
#^^^^^^^^^#
#^@@@@@@@^#
#^@@@@@@@^#
#^@@@@@@@^#
#^^^^^^^^^#
###########

Author:  Nathan4102 [ Sun Aug 11, 2013 7:41 pm ]
Post subject:  RE:A Namesless Game - Project Progress Thread

Ahhhh, I see. Seems simple enough, Ill get on it!

Author:  Nathan4102 [ Sat Aug 17, 2013 4:21 pm ]
Post subject:  RE:A Namesless Game - Project Progress Thread

Well that didn't last long...

This project's going on hiatus for a short time because I simply don't have time to work on it. I have a job, sports are starting up, AND I need to get the gist of OOP and the basics of Java down by September.

To be continued...

Author:  Nathan4102 [ Sun Sep 15, 2013 4:17 pm ]
Post subject:  Re: A Namesless Game - Project Progress Thread

Its back!

I've worked quite a few hours on this since my last update, and the gameplay mechanics are starting to come together now! The existing code has been made much more efficient aswell. For some reason, I can't edit my first post, and I don't want to post another HUGE block of code, so I'll just post a pastebin link and my RAR.

Next steps are to add a border around the game to hide the ugly animation effect, and then start working on the map some more. I'd also like to start working on an animated character that moves while idle/walking.

Let me know what you guys think! Anything you guys think can be improved on? Any ways I can make my code neater/more efficient?

Thanks!

Code: http://pastebin.com/rPwwmLEb


: