source: ttyvaders/trunk/main.c @ 12

Last change on this file since 12 was 12, checked in by Sam Hocevar, 18 years ago
  • First commit. Scrolling works, some weaponry, controls, tunnel blowing, the nuke is still ugly, almost no collision detection. Nice demo :)
File size: 4.2 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
61    starfield *sf = malloc(sizeof(starfield));
62    weapons *wp = malloc(sizeof(weapons));
63    explosions *ex = malloc(sizeof(explosions));
64    tunnel *t = create_tunnel( g, g->w, g->h );
65    player *p = create_player( g );
66    aliens *al = malloc(sizeof(aliens));
67
68    init_starfield( g, sf );
69    init_weapons( g, wp );
70    init_explosions( g, ex );
71    init_aliens( g, al );
72
73    /* Temporary stuff */
74    for( i = 0; i < 5; i++ )
75    {
76        add_alien( g, al, rand() % g->w, rand() % g->h / 2 );
77    }
78    t->w = 25;
79
80    while( !quit )
81    {
82        char key;
83
84        while( ( key = get_key() ) )
85        {
86            switch( key )
87            {
88                case 'q':
89                    quit = 1;
90                    break;
91                case 'p':
92                    poz = !poz;
93                    break;
94                case 's':
95                    skip = 1;
96                    break;
97                case 'h':
98                    p->dir = -3;
99                    break;
100                case 'j':
101                    //if( p->y < g->h - 2 ) p->y += 1;
102                    break;
103                case 'k':
104                    //if( p->y > 1 ) p->y -= 1;
105                    break;
106                case 'l':
107                    p->dir = 3;
108                    break;
109                case '\r':
110                    add_explosion( g, ex, p->x + 2, p->y, 0, 0, 2 );
111                    break;
112                case ' ':
113                    if( p->weapon == 0 )
114                    {
115                        p->weapon = 4;
116                        add_weapon( g, wp, p->x, p->y );
117                        add_weapon( g, wp, p->x + 5, p->y );
118                    }
119                    break;
120            }
121        }
122
123        usleep(40000);
124
125        if( GET_RAND(0,10) == 0 )
126        {
127            add_alien( g, al, 0, rand() % g->h / 2 );
128        }
129
130        if( poz )
131        {
132            if( skip )
133            {
134                skip = 0;
135            }
136            else
137            {
138                continue;
139            }
140        }
141
142        /* Scroll and update positions */
143        collide_player_tunnel( g, p, t, ex );
144        update_player( g, p );
145        collide_player_tunnel( g, p, t, ex );
146
147        update_starfield( g, sf );
148        update_aliens( g, al );
149
150        collide_weapons_tunnel( g, wp, t, ex );
151        collide_weapons_aliens( g, wp, al, ex );
152        update_weapons( g, wp );
153        collide_weapons_tunnel( g, wp, t, ex );
154        collide_weapons_aliens( g, wp, al, ex );
155
156        update_explosions( g, ex );
157        update_tunnel( g, t );
158
159        /* Clear screen */
160        clear_graphics();
161
162        /* Print starfield, tunnel, aliens, player and explosions */
163        draw_starfield( g, sf );
164        draw_tunnel( g, t );
165        draw_aliens( g, al );
166        draw_player( g, p );
167        draw_weapons( g, wp );
168        draw_explosions( g, ex );
169
170        /* Refresh */
171        refresh_graphics();
172    }
173
174#if 0
175    free_player( p );
176    free_tunnel( t );
177#endif
178}
179
Note: See TracBrowser for help on using the repository browser.