source: ttyvaders/trunk/src/main.c @ 24

Last change on this file since 24 was 24, checked in by Sam Hocevar, 18 years ago
  • new weapon: bomb.
  • beam now collides with aliens.
File size: 6.5 KB
Line 
1/*
2 *   ttyvaders - a tty based shoot'em'up
3 *   Copyright (C) 2002 Sam Hocevar <sam@zoy.org>
4 *
5 *   This program is free software; you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation; either version 1, or (at your option)
8 *   any later version.
9 *
10 *   This program is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with this program; if not, write to the Free Software
17 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 */
20
21#include <stdio.h>
22#include <stdlib.h>
23
24#include <string.h>
25#include <unistd.h>
26
27#include "common.h"
28
29static void start_game (game *);
30
31int main (int argc, char **argv)
32{
33    game *g = malloc(sizeof(game));
34
35    //srand(time(NULL));
36
37    if( init_graphics() )
38    {
39        return 1;
40    }
41
42    /* Initialize our program */
43    init_game(g);
44
45    /* Go ! */
46    start_game(g);
47
48    /* Clean up */
49    end_graphics();
50
51    return 0;
52}
53
54static void start_game (game *g)
55{
56    int i;
57    int quit = 0;
58    int poz = 0;
59    int skip = 0;
60    int purcompteur = 0;
61
62    g->sf = malloc(sizeof(starfield));
63    g->wp = malloc(sizeof(weapons));
64    g->ex = malloc(sizeof(explosions));
65    g->bo = malloc(sizeof(bonus));
66    g->t = create_tunnel( g, g->w, g->h );
67    g->p = create_player( g );
68    g->al = malloc(sizeof(aliens));
69
70    init_starfield( g, g->sf );
71    init_weapons( g, g->wp );
72    init_explosions( g, g->ex );
73    init_aliens( g, g->al );
74
75    /* Temporary stuff */
76    for( i = 0; i < 5; i++ )
77    {
78        add_alien( g, g->al, rand() % g->w, rand() % g->h / 2, ALIEN_POOLP );
79    }
80
81    g->t->w = 25;
82
83    while( !quit )
84    {
85        char key;
86
87        while( ( key = get_key() ) )
88        {
89            switch( key )
90            {
91                case 'q':
92                    quit = 1;
93                    break;
94                case 'p':
95                    poz = !poz;
96                    break;
97                case 's':
98                    skip = 1;
99                    break;
100                case 'h':
101                    g->p->dir = -3;
102                    break;
103                case 'j':
104                    if( g->p->y < g->h - 2 ) g->p->y += 1;
105                    break;
106                case 'k':
107                    if( g->p->y > 1 ) g->p->y -= 1;
108                    break;
109                case 'l':
110                    g->p->dir = 3;
111                    break;
112                case 'n':
113                    if( g->p->nuke == 0 )
114                    {
115                        g->p->nuke = 40;
116                        add_weapon( g, g->wp, (g->p->x + 2) << 4, g->p->y << 4, 0, 0, WEAPON_NUKE );
117                    }
118                    break;
119                case '\r':
120                    if( g->p->nuke == 0 )
121                    {
122                        g->p->nuke = 40;
123                        add_weapon( g, g->wp, (g->p->x + 2) << 4, g->p->y << 4, 0, 0, WEAPON_BEAM );
124                    }
125                    break;
126                case 'b':
127                    if( g->p->weapon == 0 )
128                    {
129                        g->p->weapon = 4;
130                        add_weapon( g, g->wp, (g->p->x + 2) << 4, g->p->y << 4, 0, -16, WEAPON_BOMB );
131                    }
132                case ' ':
133                    if( g->p->weapon == 0 )
134                    {
135                        g->p->weapon = 4;
136                        add_weapon( g, g->wp, g->p->x << 4, g->p->y << 4, 0, -16, WEAPON_LASER );
137                        add_weapon( g, g->wp, (g->p->x + 5) << 4, g->p->y << 4, 0, -16, WEAPON_LASER );
138                        /* Extra shtuph */
139                        add_weapon( g, g->wp, g->p->x << 4, g->p->y << 4, -24, -16, WEAPON_SEEKER );
140                        add_weapon( g, g->wp, (g->p->x + 5) << 4, g->p->y << 4, 24, -16, WEAPON_SEEKER );
141                        /* More shtuph */
142                        add_weapon( g, g->wp, (g->p->x + 1) << 4, (g->p->y - 1) << 4, 0, -16, WEAPON_LASER );
143                        add_weapon( g, g->wp, (g->p->x + 4) << 4, (g->p->y - 1) << 4, 0, -16, WEAPON_LASER );
144                        /* Even more shtuph */
145                        add_weapon( g, g->wp, (g->p->x + 2) << 4, (g->p->y - 1) << 4, 0, -16, WEAPON_LASER );
146                        add_weapon( g, g->wp, (g->p->x + 3) << 4, (g->p->y - 1) << 4, 0, -16, WEAPON_LASER );
147                        /* Extra shtuph */
148                        add_weapon( g, g->wp, g->p->x << 4, g->p->y << 4, -32, 0, WEAPON_SEEKER );
149                        add_weapon( g, g->wp, (g->p->x + 5) << 4, g->p->y << 4, 32, 0, WEAPON_SEEKER );
150                    }
151                    break;
152            }
153        }
154
155        usleep(40000);
156
157        if( !poz || skip )
158        {
159            skip = 0;
160
161            /* XXX: to be removed */
162            if( GET_RAND(0,10) == 0 )
163            {
164                int list[3] = { ALIEN_POOLP, ALIEN_BOOL, ALIEN_BRAH };
165
166                add_alien( g, g->al, 0, rand() % g->h / 2, list[GET_RAND(0,3)] );
167            }
168
169            /* Update game rules */
170            if( g->t->right[1] - g->t->left[1] == g->t->w )
171            {
172                g->t->w = 85 - g->t->w;
173            }
174
175            /* Scroll and update positions */
176            collide_player_tunnel( g, g->p, g->t, g->ex );
177            update_player( g, g->p );
178            collide_player_tunnel( g, g->p, g->t, g->ex );
179
180            update_starfield( g, g->sf );
181            update_bonus( g, g->bo );
182            update_aliens( g, g->al );
183
184            collide_weapons_tunnel( g, g->wp, g->t, g->ex );
185            collide_weapons_aliens( g, g->wp, g->al, g->ex );
186            update_weapons( g, g->wp );
187            collide_weapons_tunnel( g, g->wp, g->t, g->ex );
188            collide_weapons_aliens( g, g->wp, g->al, g->ex );
189
190            update_explosions( g, g->ex );
191            /*if(purcompteur%2)*/ update_tunnel( g, g->t );
192        }
193
194        /* Clear screen */
195        clear_graphics();
196
197        /* Print starfield, tunnel, aliens, player and explosions */
198        draw_starfield( g, g->sf );
199        draw_tunnel( g, g->t );
200        draw_bonus( g, g->bo );
201        draw_aliens( g, g->al );
202        draw_player( g, g->p );
203        draw_explosions( g, g->ex );
204        draw_weapons( g, g->wp );
205
206        /* Refresh */
207        refresh_graphics();
208
209        purcompteur++;
210    }
211
212#if 0
213    free_player( p );
214    free_tunnel( g->t );
215#endif
216}
217
Note: See TracBrowser for help on using the repository browser.