Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include "cab202_graphics.h"
- #include "cab202_timers.h"
- #include "cab202_sprites.h"
- sprite_id alien[10];
- sprite_id player;
- timer_id tim;
- timer_id drop;
- sprite_id bullet;
- sprite_id bomb[4];
- int spot;
- int life = 3;
- int SCORE = 0;
- int quit = 0;
- bool shoot = false;
- int count = 0;
- void info() {
- int x = screen_width()/2;
- draw_string(x - 5, screen_height() -1, "Level: 1" );
- draw_string(0, screen_height()-2, "Georgina Hine (n8872597)");
- draw_string(screen_width() - 11, screen_height() - 2, "Lives:");
- draw_string(screen_width()-22, screen_height() - 2, "Score:");
- draw_line(0, screen_height() - 3, screen_width(), screen_height() - 3, '-');
- draw_int(screen_width() - 4, screen_height() - 2, life);
- draw_int(screen_width() - 15, screen_height() - 2, SCORE);
- show_screen();
- }
- void setup() {
- setup_screen();
- player = create_sprite(screen_width()/2, screen_height() - 5, 1, 1, "S");
- player->dy = 0;
- player->dx = 0;
- bullet = create_sprite(-1, -1, 1, 1, "*");
- bullet->dy = 0;
- bullet->dx = 0;
- for (int b = 0; b< 4; b++){
- bomb[b] = create_sprite(8, 9, 1, 1, "&");
- bomb[b]->dx = 0;
- bomb[b]->dy = 0;
- bomb[b]->is_visible = false;
- }
- for (int i = 0; i < 3; i++) {
- alien[i] = create_sprite(4+4*i, 0, 1, 1, "@");
- }
- for (int j = 0; j < 4; j++) {
- alien[j+3] = create_sprite(2+4*j, 3, 1, 1, "@");
- }
- for (int k = 0; k < 3; k++) {
- alien[k+7] = create_sprite(4+4*k, 6, 1, 1, "@");
- }
- for (int r = 0; r < 10; r++) {
- alien[r]->dx = 0.1;
- alien[r]->dy = 0;
- }
- tim = create_timer(25);
- drop = create_timer(3000);
- srand(time(NULL));
- }
- void draws() {
- clear_screen();
- draw_sprite(bullet);
- draw_sprite(player);
- for (int i = 0; i < 10; i++) {
- draw_sprite(alien[i]);
- }
- for(int j = 0; j < 4; j++) {
- draw_sprite(bomb[j]);
- }
- show_screen();
- }
- void check_input() {
- int key = get_char();
- if (key == 'a'){
- player->dx = -1;
- }
- else if (key == 'd'){
- player->dx = 1;
- }
- else if (key == 'q'){
- quit = 1;
- }
- else if (key == 's'){
- if(shoot == false) {
- bullet->y = screen_height() - 5;
- bullet->dy = -1;
- spot = player->x;
- shoot = true;
- }
- }
- else {
- player->dx = 0;
- player->dy = 0;
- }
- while (get_char() != -1) {
- }
- }
- void update_state() {
- //Check if we've hit the boundaries
- if (player->x + player->dx < 0 || player->x + player->dx >= screen_width()){
- player->dx = 0;
- player->dy = 0;
- }
- //Move player
- player->x += player->dx;
- player->y +=player->dy;
- //Reset move vector
- player->dx = 0;
- player->dy = 0;
- if (shoot == true) {
- bullet->y += bullet->dy;
- bullet->x = spot;
- }
- else {
- bullet->dy = 0;
- bullet->x = -1;
- bullet->y = -1;
- }
- if (bullet->y < 0) {
- shoot = false;
- }
- for (int i = 0; i < 10; i++) {
- if (alien[i]->x + alien[i]->dx > screen_width() - 1) {
- alien[i]->x = -1;
- }
- }
- for (int r = 0; r < 10; r++) {
- alien[r]->x += alien[r]->dx ;
- alien[r]->y += alien[r]->dy;
- }
- for (int l = 0; l<10; l++) {
- if (round(bullet->x) == round(alien[l]->x) && round(bullet->y) + round(bullet->dy) == round(alien[l]->y)) {
- alien[l]->x = -1;
- alien[l]->y = -3;
- alien[l]->dy= 0;
- alien[l]->dx = 0;
- SCORE = SCORE + 30;
- shoot = false;
- count = count + 1;
- }
- }
- for(int z = 0; z < 4; z++) {
- if(round(bomb[z]->x) == round(player->x) && round(bomb[z]->y) + round(bomb[z]->dy) == round(player->y)) {
- life = life - 1;
- bomb[z]->is_visible = false;
- }
- }
- for(int f = 0; f < 4; f++) {
- if(bomb[f]->x == player->x + player->dx && bomb[f]->y == player->dy + player->y) {
- bomb[f]->is_visible = false;
- }
- }
- for(int z = 0; z < 4; z++) {
- if(bomb[z]->y > screen_height() - 4) {
- bomb[z]->is_visible = false;
- }
- }
- if (timer_expired(drop)) { //make the random number smaller as the number of aliens decreases
- int c = rand() %10;
- int h = rand() %4;
- if (bomb[h]->is_visible == false) {
- bomb[h]->dy = 0.3;
- bomb[h]->x = alien[c]->x + bomb[h]->dx;
- bomb[h]->y = alien[c]->y + bomb[h]->dy;
- bomb[h]->is_visible = true;
- }
- reset_timer(drop);
- }
- for(int z = 0; z < 4; z++) {
- if(bomb[z]->is_visible == true) {
- bomb[z]->y += bomb[z]->dy;
- }
- else {
- bomb[z]->dy = 0;
- bomb[z]->x = -3;
- bomb[z]->y = -4;
- }
- }
- int no = 10;
- if(count == no) {
- for (int r = 0; r < 10; r++) {
- alien[r]->dx = 0.1;
- alien[r]->dy = 0;
- }
- for (int i = 0; i < 3; i++) {
- alien[i]->x = 4+4*1;
- alien[i]->y = 0; //= create_sprite(4+4*i, 0, 1, 1, "@");
- }
- for (int j = 0; j < 4; j++) {
- alien[j+3]->x = 2+4*j;
- alien[j+3]->y = 3; //= create_sprite(2+4*j, 3, 1, 1, "@");
- }
- for (int k = 0; k < 3; k++) {
- alien[k+7]->x = 4+4*k;
- alien[k+7]->y = 6; //= create_sprite(4+4*k, 6, 1, 1, "@");
- }
- no = no + 10;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement