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

Last change on this file since 20 was 20, checked in by Sam Hocevar, 18 years ago
  • real Debian package files.
  • two new types of aliens. only one rules.
  • removed gfx_write in favor of gfx_putchar and gfx_putstr.
  • added bonuses at alien death. they do nothing yet.
  • seeker missiles. 'b' to test.
  • weapon resolution is now 16*char. needs to be generalized.
  • fixed the supernova bugs. center could collide with the tunnel, and the last frame was badly displayed.
  • lots of cleanups everywhere.
File size: 5.3 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 '\r':
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 'b':
120                    if( g->p->weapon == 0 )
121                    {
122                        g->p->weapon = 4;
123                        add_weapon( g, g->wp, g->p->x << 4, g->p->y << 4, -24, -16, WEAPON_SEEKER );
124                        add_weapon( g, g->wp, (g->p->x + 5) << 4, g->p->y << 4, 24, -16, WEAPON_SEEKER );
125                    }
126                case ' ':
127                    if( g->p->weapon == 0 )
128                    {
129                        g->p->weapon = 4;
130                        add_weapon( g, g->wp, g->p->x << 4, g->p->y << 4, 0, -16, WEAPON_LASER );
131                        add_weapon( g, g->wp, (g->p->x + 5) << 4, g->p->y << 4, 0, -16, WEAPON_LASER );
132                    }
133                    break;
134            }
135        }
136
137        usleep(40000);
138
139        if( GET_RAND(0,10) == 0 )
140        {
141            int list[3] = { ALIEN_POOLP, ALIEN_BOOL, ALIEN_BRAH };
142
143            add_alien( g, g->al, 0, rand() % g->h / 2, list[GET_RAND(0,3)] );
144        }
145
146        if( poz )
147        {
148            if( skip )
149            {
150                skip = 0;
151            }
152            else
153            {
154                continue;
155            }
156        }
157
158        /* Update game rules */
159        if( g->t->right[1] - g->t->left[1] == g->t->w )
160        {
161            g->t->w = 85 - g->t->w;
162        }
163
164        /* Scroll and update positions */
165        collide_player_tunnel( g, g->p, g->t, g->ex );
166        update_player( g, g->p );
167        collide_player_tunnel( g, g->p, g->t, g->ex );
168
169        update_starfield( g, g->sf );
170        update_bonus( g, g->bo );
171        update_aliens( g, g->al );
172
173        collide_weapons_tunnel( g, g->wp, g->t, g->ex );
174        collide_weapons_aliens( g, g->wp, g->al, g->ex );
175        update_weapons( g, g->wp );
176        collide_weapons_tunnel( g, g->wp, g->t, g->ex );
177        collide_weapons_aliens( g, g->wp, g->al, g->ex );
178
179        update_explosions( g, g->ex );
180        /*if(purcompteur%2)*/ update_tunnel( g, g->t );
181
182        /* Clear screen */
183        clear_graphics();
184
185        /* Print starfield, tunnel, aliens, player and explosions */
186        draw_starfield( g, g->sf );
187        draw_tunnel( g, g->t );
188        draw_bonus( g, g->bo );
189        draw_aliens( g, g->al );
190        draw_player( g, g->p );
191        draw_weapons( g, g->wp );
192        draw_explosions( g, g->ex );
193
194        /* Refresh */
195        refresh_graphics();
196
197        purcompteur++;
198    }
199
200#if 0
201    free_player( p );
202    free_tunnel( g->t );
203#endif
204}
205
Note: See TracBrowser for help on using the repository browser.