Thread: Space Invaders

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    1

    Question Space Invaders

    Hi everyone,
    im writing a c code project called space invaders, but
    ive got a problem with it.
    I want to delete an enemy if it was hit.
    I have no idea, how can i realize it, to overwrite the array position.
    Here is my source code...

    Code:
    	while (1) {		//Bildschirm reset und Startposition
    		system("cls");
    		for (y = 1; y <= 20; y++) {
    			for (x = 1; x <= 30; x++) {
    
    
    				if (x == 1 || x == 30 || y == 1 || y == 20) {
    					feld[y][x] = h_muster;
    				}
    				else if (feld[y][x] == 'O' && feld[y+2][x] == 'e') {
    					//Feldwerte in Array schreiben
    					
    					feld[y][x] = '.';
    					score++;
    				}
    				else if (feld[y][x] == '^') {
    					feld[y][x] = 'e';
    					feld[y-1][x] = '^';
    				}
    				else if (x == p_cursor_x && y == 18 && shot == 1) {
    					//Drucke Schuss
    					feld[y][x] = '^';
    					shot = 0;
    				}
    				else if (x > 4 && x < 27 && y > scroll_down && y < (scroll_down + 6)) {
    					//Drucke Gegner
    					feld[y][x] = 'O';
    					cycles++;
    					if (cycles == 10000) {
    						scroll_down++;
    						cycles = 0;
    					}
    				}
    				
    				else if (x == p_cursor_x && y == 19) {
    					//Drucke Spieler
    					feld[y][x] = 'T';
    				}
    				else {
    					feld[y][x] = '.';
    				}
    
    
    
    
    				printf("%c", feld[y][x]);
    			}printf("\n");
    
    
    		}
    		printf("\n\nSCORE: %d", score);
    
    
    		//Einlesen von Taste, falls Tasteneingabe nicht erfolgt -> Leerzeichen übergeben
    		if (_kbhit()) {
    			read_keyboard(_getch());
    			fflush(stdin);
    		}
    		else {
    			read_keyboard(' ');
    		}
    
    
    
    
    		//Warte 50ms, damit Bild nicht so oft neu geladen wird.
    		Sleep(50);
    	}

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I don't understand why you can't just overwrite the array element. What's the problem?

    system("cls") is a very inefficient mechanism to clear the screen. To clear the screen efficiently see the second option here: Clearing the console screen (Windows)

    In fact, using the Windows console functions you could write directly to whatever position you want so you wouldn't need to clear the entire screen.

    Although in your situation it probably doesn't matter that fflush(stdin) is "undefined behaviour" according to the C standard (as I believe it has some functionality in Windows), it does seem unnecessary to use it after a _getch call. I'm not even sure if it would work since _getch doesn't use stdin but reads from the keyboard buffer directly. If you really need to remove any extra keystrokes, then something like while(kbhit()) _getch(); would probably work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create Letter Invaders typing game in c language?
    By beginner_babe in forum C Programming
    Replies: 41
    Last Post: 01-12-2014, 10:49 AM
  2. Space Invaders. Both shoot and move at once?
    By Swerve in forum Game Programming
    Replies: 13
    Last Post: 10-25-2008, 10:58 AM
  3. Collision problems with Space Invaders
    By dxfoo in forum Game Programming
    Replies: 3
    Last Post: 03-25-2006, 12:21 AM
  4. Need help on my Space Invaders Game
    By zz3ta in forum Game Programming
    Replies: 6
    Last Post: 01-04-2004, 04:32 PM
  5. Galaxian or Space invaders for my next game (read this)
    By Leeman_s in forum C++ Programming
    Replies: 7
    Last Post: 11-07-2001, 09:28 PM

Tags for this Thread