Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

lorductape

macrumors 6502
Original poster
here's the code for "handball" which I wrote for my programming class.

go to town with it! (so long as you post your results =])

oh and you need to compile and run it in windows, i used DevCpp. (i'm a diehard mac fanboy, but my school uses windows =[ )

Code:
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>

/*Handball by Daniel S.
      I think this is the version that works. if not then FIX IT! =] 
      feel free to modify and change as long as you post the result.
*/

using namespace std;

        

//mygraphics.h functions
bool setlocation(int x, int y){
    static HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD c;
    c.X = x;
    c.Y = y;
    SetConsoleCursorPosition(console, c);
}
bool setcolor(int c){
     static HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);   
     SetConsoleTextAttribute(console, c); 
}
void delay(float seconds){
     float sec = (float)(seconds*10);
     Sleep(sec*100);
}

//displays the chart on the screen
void draw_chart(){
    setcolor(15);
    cout<<(char)201;
     for (int i = 1; i <= 50; i++){
         setlocation(i,0);
         cout<<(char)205;
     }
     for (int i = 1; i <= 50; i++){
         setlocation(i,20);
         cout<<(char)205;
     }
     for (int i = 0; i <= 18; i++){
         setlocation(0,i+1);
         cout<<(char)186;
     }
     for (int i = 0; i <= 18; i++){
         setlocation(40,i+1);
         cout<<(char)186;
     }
     for (int i = 0; i <= 18; i++){
         setlocation(50,i+1);
         cout<<(char)186;
     }
     setlocation(40,0);
     cout<<(char)203;
     setlocation(0,20);
     cout<<(char)200;
     setlocation(40,20);
     cout<<(char)202;
     setlocation(50,0);
     cout<<(char)187;
     setlocation(50,20);
     cout<<(char)188;
     return;
}

//what happens when a key is pressed
void action(int &pad_pos,float &speed){
     int key = getch();
     
     
     
     
     
     
     if (key == (char)'a'){
        setlocation(pad_pos-1,19);
        cout<<" ";
        setlocation(pad_pos+1,19);
        cout<<" ";
        setlocation(pad_pos,19);
        cout<<" ";
        setlocation(pad_pos+2,19);
        cout<<" ";
        setlocation(pad_pos-2,19);
        cout<<" ";
        pad_pos -= 1;
        if (pad_pos == 2)
           pad_pos = 37;
        setlocation(pad_pos,19);
        cout<<(char)220;
        setlocation(pad_pos+1,19);
        cout<<(char)220;
        setlocation(pad_pos-1,19);
        cout<<(char)220;
        setlocation(pad_pos+2,19);
        cout<<(char)220;
        setlocation(pad_pos-2,19);
        cout<<(char)220;   
     }
     
     if (key == (char)'d'){  
        setlocation(pad_pos-1,19);
        cout<<" ";
        setlocation(pad_pos+1,19);
        cout<<" ";
        setlocation(pad_pos,19);
        cout<<" ";
        setlocation(pad_pos+2,19);
        cout<<" ";
        setlocation(pad_pos-2,19);
        cout<<" ";
        pad_pos += 1;
        if (pad_pos == 38)
           pad_pos = 3;
        setlocation(pad_pos,19);
        cout<<(char)220;
        setlocation(pad_pos+1,19);
        cout<<(char)220;
        setlocation(pad_pos-1,19);
        cout<<(char)220;
        setlocation(pad_pos+2,19);
        cout<<(char)220;
        setlocation(pad_pos-2,19);
        cout<<(char)220;   
        
     }
     if (key == (char)'w'){
        if(speed != .002)
            speed -= .001;
     }
     
     if (key == (char)'s'){
        if(speed != .009)
        speed += .001;
     }
     if (key == (char)' '){
        getch();
        return;
     }
     return;
}

//the code to move the ball
void bounce(int x, int y, int &pad_pos, float &speed, int &lives, int &score){

     char vert = 'u';
     char horz = 'r';
     bool lose = false;
     do{
        int prex = x;
        int prey = y;

        setcolor(15);

        setlocation(41,1);
        cout<<"Handball";
        setlocation(41,2);
        cout<<"By DS";
        setlocation(41,4);
        cout<<"Speed:";
        setlocation(43,5);
        cout<<"     ";
        setlocation(43,5);
        cout<<speed*1000;    
        setlocation(41,7);
        cout<<"Lives:";
        setlocation(41,8);
        cout<<lives;
        setlocation(41,9);
        cout<<"Score:";
        setlocation(43,10);
        cout<<score;

                
        setlocation(pad_pos,19);
        cout<<(char)220;
        setlocation(pad_pos+1,19);
        cout<<(char)220;
        setlocation(pad_pos-1,19);
        cout<<(char)220;
        setlocation(pad_pos+2,19);
        cout<<(char)220;
        setlocation(pad_pos-2,19);
        cout<<(char)220;

        
        if (vert == 'd')
           y += 1;
        if (vert == 'u')
           y -= 1;
        if (horz == 'l')
           x -= 1;
        if (horz == 'r')
           x += 1;
        if (vert == 'u' && y == 2){
           vert = 'd';
           y -= 1;
        }
        if (vert == 'd' && y == 18){
           lose = true;
        }
        if (horz == 'l' && x == 0){
           horz = 'r';
           x += 1;
        }
        if (horz == 'r' && x == 40){
           horz = 'l';
           x -= 1;
        }
        if (y==17 && (x == pad_pos||x == pad_pos+1||x == pad_pos-1||x == pad_pos+2||x == pad_pos-2||x == pad_pos+3||x == pad_pos-3)){
           vert = 'u';
           y += 1;
           if(horz == 'r')
                x++;
            if(horz == 'l')
                x--;
           score += ((10-(speed*1000))*5);
        }
      for(int p = 0; p <= 15; p++){  
        if (kbhit()){
              action(pad_pos,speed);
        }
        delay(speed);
    }
        setlocation(prex,prey);
        cout<<" ";
            setcolor(13);
        setlocation(x,y);
        cout<<"*";
        }while(lose == !true);   


}

//welcome screen
void intro(){
     cout<<"Welcome to handball.";
     cout<<"\n\nControls:";
     cout<<"\n\ta/d-left/right";
     cout<<"\n\tw/s-condtrol speed"; 
     cout<<"\n\tspace-pause"; 
     delay(3);
     system("CLS");
}


void count_before(int &pad_pos, float &speed, int &score, int &lives){
    setlocation(41,1);
        cout<<"Handball";
        setlocation(41,2);
        cout<<"By DS";
        setlocation(41,4);
        cout<<"Speed:";
        setlocation(43,5);
        cout<<"     ";
        setlocation(43,5);
        cout<<speed*1000;    
        setlocation(41,7);
        cout<<"Lives:";
        setlocation(41,8);
        cout<<lives;
        setlocation(41,9);
        cout<<"Score:";
        setlocation(43,10);
        cout<<score;
        setlocation(pad_pos,19);
        cout<<(char)220;
        setlocation(pad_pos+1,19);
        cout<<(char)220;
        setlocation(pad_pos-1,19);
        cout<<(char)220;
        setlocation(pad_pos+2,19);
        cout<<(char)220;
        setlocation(pad_pos-2,19);
        cout<<(char)220;
  for(int i = 3; i >= 1; i--){
    
    if(i == 3){
        setlocation(12,5);
        cout<<(char)219<<(char)219<<(char)219;
        setlocation(14,6);
        cout<<(char)219;
        setlocation(12,7);
        cout<<(char)219<<(char)219<<(char)219;
        setlocation(14,8);
        cout<<(char)219;        
        setlocation(12,9);
        cout<<(char)219<<(char)219<<(char)219;        
    }
    if(i == 2){
        setlocation(12,5);
        cout<<(char)219<<(char)219<<(char)219;
        setlocation(14,6);
        cout<<(char)219;
        setlocation(12,7);
        cout<<(char)219<<(char)219<<(char)219;
        setlocation(12,8);
        cout<<(char)219<<"  ";        
        setlocation(12,9);
        cout<<(char)219<<(char)219<<(char)219;        
    }
    if(i == 1){
        setlocation(12,5);
        cout<<" "<<(char)219<<" ";
        setlocation(12,6);
        cout<<" "<<(char)219<<" ";
        setlocation(12,7);
        cout<<" "<<(char)219<<" ";                
        setlocation(12,8);
        cout<<" "<<(char)219<<" ";
        setlocation(12,9);
        cout<<" "<<(char)219<<" ";  
    }              
        for(int p = 0; p <= 10; p++){  
        if (kbhit()){
              action(pad_pos,speed);
        }
    delay(.08);
    }
}
}

//...
int main()
{
    srand(time(NULL));
    intro();

    int x = 1;
    int y = 1;
    int pad_pos = 18;
    draw_chart();
    int lives = 9;
    int score = 0;
    float speed = .009;
    for(lives = 9; lives != 0; lives--){
          system("CLS");
          draw_chart();
          count_before(pad_pos,speed,score,lives);
            system("CLS");
          draw_chart();
          x = pad_pos;
          y = 18;
          bounce(x,y,pad_pos,speed,lives,score);
          score -= 20;
          system("CLS");
    }
    system("CLS");
    cout<<"You have lost!";
    getch();
    return EXIT_SUCCESS;
}

anyone??
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.