Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411126 Posts in 69302 Topics- by 58376 Members - Latest Member: TitanicEnterprises

March 13, 2024, 01:42:01 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
  Show Posts
Pages: [1]
1  Developer / Technical / Re: Producing .exe by code on: February 17, 2012, 01:41:38 PM
I would just do this:

Code:
// main.cpp

#include "interpreter_library.h"

const char* script_code = "
# script

print 'Hello, world!'

stuff()
foo()
";

int main(int argc, char** argv)
{
    interpret_script_from_string(script_code);

    return 0;
}

I hope it's clear Shrug

The problem with this approach is that you have to recompile it every time you change the script.

~Flops
2  Developer / Technical / Re: A c++ approach to snake - 61 lines total - ncurses on: February 17, 2012, 01:06:35 PM
I added the std::pair stuff, didn't know about the possibility of that. In Stroustrup's book the section about make_pair() and std::pair() is very shot - I looked it up.

The newX and newY were added as well, makes everything clear.

Thanks again

~Flops

By the way: It's 66 lines of code now. It doesn't make sense to rewrite the switch to multiple ?:. At least in my eyes.
3  Developer / Technical / Re: Producing .exe by code on: February 17, 2012, 12:43:27 PM
Let me try to explain it again:

1) You write your parser which reads a txt file and executes it accordingly.
2) You write a program which opens the exe using ios::binary and the txt file using ios:binary.
3) You concatenate both strings.
4) The resulting string is written to a file called 'new.exe'.
5) You launch 'new.exe'.
6) The behaviour is the same as 1) but the end of the exe is actually your text file.
7) You change your parser to open itself ('new.exe') instead of a text file.
8) The string which contains the data from the exe contains the executable data and the txt contents (which both is in 'new.exe' because you wrote it there earlier) gets stripped so it only contains the txt data.
9) Use the resulting string as you used the txt input before.
10) Party.

I tried this method and it works.

~Flops
4  Developer / Technical / Re: Producing .exe by code on: February 17, 2012, 05:50:45 AM
If you have your interpreter in your exe you can just append data to the end of the exe file. When executed the interpreter opens the file using argv[0] and looks at the end of the file.

To know how much data you have to read you could append the number at the end, when generating that file.
5  Developer / Technical / Re: A c++ approach to snake - 100 lines total - ncurses on: February 14, 2012, 11:47:43 AM
Wow, many thanks for all the help. I tried to incorporate as much as I can.

First I changed the includes:
Code:
#include <list>
#include <ncurses.h>
#include <cstdlib>
#include <ctime>


After that I changed the class to a struct. I didn't know it's possible to have initialization lists in a structure.
Also, I wasn't aware you can use parameters which have got the same name as member variables.
Code:
struct snake_point {
  int x, y;
  snake_point(int x, int y):x(x),y(y) {}
};


Of course I had to change the methods calls, too.


compression! -->
Code:
switch( ch ) {
        case KEY_UP:    y--; break;
        case KEY_DOWN:  y++; break;
        case KEY_RIGHT: x++; break;
        case KEY_LEFT:  x--; break;
    }
-->
Code:
y += ch==KEY_UP?1:ch==KEY_DOWN?-1:0;
x += ch==KEY_RIGHT?1:ch==KEY_LEFT?-1:0;


I think I can't use this as is because the source code above would only move the snake when a key is pressed. I have to save the direction somewhere.
So I came up with:
Code:
dir = (ch == KEY_UP) ? 1 : (ch == KEY_RIGHT) ? 2 : (ch == KEY_DOWN) ? 3 : (ch == KEY_LEFT) ? 4 : dir;
quit = (ch == 'q') ? true : false;

and

Code:
int x = logic.x;
int y = logic.y;
x += (dir == 2) ? 1 : (dir == 4) ? -1 : 0;
y += (dir == 3) ? 1 : (dir == 1) ? -1 : 0;

I think it's always a good idea to split input and logic: Input -> Logic -> Output.

I couldn't have done those improvements and the compression without you, thanks again.

~Flops

EDIT: I sent the changes to github, https://github.com/FlopsKa/cppNcursesSnake .
EDIT 2: According to cloc.pl it's 61 lines of code. Is it possible to go below 50?
6  Developer / Technical / Re: A c++ approach to snake - 100 lines total - ncurses on: February 12, 2012, 02:48:55 AM

There's only one place i'd "improve" (change).

Code:
    switch(ch) {
    case KEY_UP:
    dir = 1;
    break;
    case KEY_RIGHT:
    dir = 2;
    break;
    case KEY_DOWN:
    dir = 3;
    break;
    case KEY_LEFT:
    dir = 4;
    break;
    case 'q':
    quit = true;
    break;
    }
   
    // Logic
    snake logic = snakes.front();
    int x = logic.getX();
    int y = logic.getY();
    if(dir == 1) y--; // move up
    else if(dir == 2) x++; // move right
    else if(dir == 3) y++; // move down
    else if(dir == 4) x--; // move left

to

Code:
    int x=0, y=0;
    switch( ch ) {
        case KEY_UP:    y--; break;
        case KEY_DOWN:  y++; break;
        case KEY_RIGHT: x++; break;
        case KEY_LEFT:  x--; break;
    }

    x += logic.getX();
    y += logic.getY();

Beyond that, i don't think you can improve the code, but add to it. Maybe some obsticles, a bonus randomly generating fruit? Enemies that try and bump into you, but die if eaten? Also, snake is usually a game played in real time, so you might change timeout(-1) to timeout(some val > 1).

The timeout is 100, see line 25. I disable it only to show the points when the game quits, so it shows the points until the player hits a key. Else it would wait 100ms and quit which is to fast to see the points.

I don't understand the code changes: I have to save the current direction somewhere in order to be able to move the snake when no key is pressed. Your code would only work with a timeout of -1.

I don't think I'll add other stuff because I used this game only as an experiment to test ncurses.


The main thing that caught my eye is the class name here:
Code:
class snake {
private:
int x, y;
public:
snake(int a, int b) {
x = a;
y = b;
}
int getX() { return x; }
int getY() { return y; }
};
This class for all intents and purposes is basically a point, not a complete snake. You could also call it a snake part or something more fitting.



I think I'll just call it point, thanks for the hint.

Thanks for the help.

~Flops

EDIT: The source is on github now: https://github.com/FlopsKa/c---ncurses-snake
7  Developer / Technical / Re: Collision detection : Rotation snapping on: February 10, 2012, 10:24:52 AM
I did a short sketch, this is how I see your problem:


You would calculate alpha and look which angle it has approximately - then rotate it accordingly.
8  Developer / Technical / Re: Collision detection : Rotation snapping on: February 10, 2012, 10:08:27 AM
I'd try to calculate the angle between the two vectors: On each convex object you got one vector going through the point which collides and through the neighbor point. Angles which make sense would be 0°/180° (parallel) 90* and 45*. Maybe add 30° and 60°.
The math for this solution would be rather simple.
9  Developer / Technical / A c++ approach to snake - 61 lines total - ncurses on: February 10, 2012, 07:42:18 AM
Hello guys,

because I wanted to start programming a simple rogue like and I had no idea how to use ncurses I developed a small snake clone first.

Can someone look at the source code and tell me where to improve? I used a c++ approach and packed the snake into a list so it's only 100 lines total containing code + comments.

I know that I could pack it into better classes and split the code but I wanted it to stay simple and short. Mainly because I just wanted to test ncurses.

The game is playable in a 80x24 terminal. To compile it use:
Code:
g++ main.cpp -lncurses

Thanks in advance,

Flops

Here is the source code:
Code:
#include <list>
#include <ncurses.h>
#include <stdlib.h>
#include <time.h>

class snake {
private:
int x, y;
public:
snake(int a, int b) {
x = a;
y = b;
}
int getX() { return x; }
int getY() { return y; }
};

int main() {
// Init
srand ( time(NULL) );
initscr();
noecho();
curs_set(0);
keypad(stdscr, TRUE);
timeout(100);

std::list<snake> snakes;
std::list<snake>::iterator it;
bool quit = false;
int points = 0;
int dir = 2;
int food_x = rand() % 80 + 1;
int food_y = rand() % 24 + 1;
int ch;

for(int i = 0; i < 5; i++) // generate start snake
snakes.push_front(snake(3+i,3));

while(!quit) {
    // Input
    ch = getch();
    switch(ch) {
    case KEY_UP:
    dir = 1;
    break;
    case KEY_RIGHT:
    dir = 2;
    break;
    case KEY_DOWN:
    dir = 3;
    break;
    case KEY_LEFT:
    dir = 4;
    break;
    case 'q':
    quit = true;
    break;
    }
   
    // Logic
    snake logic = snakes.front();
    int x = logic.getX();
    int y = logic.getY();
    if(dir == 1) y--; // move up
    else if(dir == 2) x++; // move right
    else if(dir == 3) y++; // move down
    else if(dir == 4) x--; // move left
   
    snakes.push_front(snake(x, y));
   
    if(x == food_x && y == food_y) {
    food_x = rand() % 80;
food_y = rand() % 24;
points++;
    } else
    snakes.pop_back();
   
    if(y > 24 || x > 80 || y < 0 || x < 0) // collision with border
    quit = true;
   
    // Output
    erase();
    mvaddch(food_y,food_x,'X');
    for(it = snakes.begin(); it != snakes.end(); it++) {
    mvaddch((*it).getY(),(*it).getX(),'o');
    if((*it).getY() == y && (*it).getX() == x && it != snakes.begin()) // collision with snake
    quit = true;
    }
    mvprintw(0, 0, "You got %i points. 'q' to quit.\n", points);
    refresh();
    }
    timeout(-1);
    erase();
    mvprintw(0, 0, "You lost and gained a total of %i points.\n", points);
    refresh();
    getch(); // wait for input
   
endwin();
return 0;
}
10  Developer / Playtesting / Re: I made things on: July 18, 2011, 12:09:52 PM
In Xcode you can automatically copy the frameworks into the *.app bundle. To do this you first have to add the framework like you normally do when using it. After you added it you can go to:

Targets => Program Name => Copy Frameworks into …

You have to drag it there from the frameworks section where you added it earlier.

I made this screenshot and I really hope it helps.


~Flops

PS: I hope it helps, I use Xcode 3.2 myself so I have no idea if it still is like that in the new version.
11  Developer / Design / Re: 2D Games with Level Editors on: March 12, 2011, 11:16:29 AM
It is 2D. It doesn't use a Cartesian coordinate system, tho.
12  Developer / Design / Re: 2D Games with Level Editors on: March 10, 2011, 01:08:42 PM
Warcraft 3 had a pretty amazing level editor. It had a built in scripting-engine…
13  Developer / Design / Re: 2D Games with Level Editors on: March 09, 2011, 01:03:23 PM
Super Tux had a level editor if I remember correctly.
14  Jobs / Collaborations / Re: 2D Mining game project on: February 28, 2011, 05:25:45 AM
In my opinion you should make the sharp edges a bit rounder.
15  Jobs / Collaborations / Re: 2D Mining game project on: February 27, 2011, 09:24:46 AM
The style reminds me of clonk.
- Compared to it your concept looks a bit plain at the moment.

Best luck with your idea, I hope you find a nice team to work with.
16  Developer / Art / Re: Art on: February 23, 2011, 07:50:36 AM
I really think you should make every picture in wallpaper size! Looks cool
17  Community / Versus / Re: A new challenger appears on: February 23, 2011, 07:39:22 AM
This looks very awesome! How long have you been working on it?
Pages: [1]
Theme orange-lt created by panic