Avatar billede ultragames Nybegynder
13. april 2004 - 22:28 Der er 4 kommentarer

struct i funktion

hej eksperter.. jeg er igang med at lave et simpelt snake spil i dos.

jeg har dog løbet ind i nogle problemer når jeg skal kalde både en struct + variable ind i en funktion:

////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <iostream.h>
#include <conio.h>
#include <windows.h>
#include <vector.h>

#define NORTH 0
#define SOUTH 1
#define EAST  2
#define WEST  3

#define SCREEN_WIDTH  79
#define SCREEN_HEIGHT 25

#define ERASE 0
#define DRAW  1



struct Spiller
{
  COORD Position;
  int Direction;
};




void spilleplade();
void monster( int x, int y );
void Tjek_bane( Spiller Spiller );

void Move_spiller( Spiller & Spiller );

HANDLE hInput, hOutput;


///////////////////////////////////////////////////////////////////////////////

int main()
{
  int monster_x, monster_y;
  srand( time( NULL ) );
  monster_x = ( rand() % 78 + 2 );
  monster_y = ( rand() % 44 + 2 );
  vector < int > x;
  vector < int > y;

  spilleplade();
  monster( monster_x, monster_y );

  Spiller Spiller;


  hInput = GetStdHandle( STD_INPUT_HANDLE );
  hOutput = GetStdHandle( STD_OUTPUT_HANDLE );




  SetConsoleMode( hInput, ENABLE_PROCESSED_INPUT );

  Spiller.Position.X = SCREEN_WIDTH / 2;
  Spiller.Position.Y = SCREEN_HEIGHT / 2;


  SetConsoleCursorPosition( hOutput, Spiller.Position );

  ////////////////////////////// G*A*M*E L*O*O*P ////////////////////////////////

  while ( 1 )
  {


    INPUT_RECORD InputRecord; DWORD Events = 0;

    ReadConsoleInput( hInput, & InputRecord, 1, & Events ); // bruges til at læse det man trykker på keyboard

    int tempo = 50;
    int dir; // direction, angiver hvilken vej man bevæger sig

    if ( InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT )
    {

      dir = 2;
      Move_spiller(Spiller, dir, x, y); // HER LIGGER FEJLEN <----------




    }

    else if ( InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT )
    {
      dir = 4;



    }

    else if ( InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_UP )
    {
      dir = 1;

    }

    else if ( InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_DOWN )
    {
      dir = 3;


    }


    Sleep( tempo );
Tjek_bane( Spiller );


    for ( double j = 0; j <= x.size(); j++ )
    {

      SetConsoleCursorPosition( hOutput, Spiller.Position );
      char brik = 219;
      gotoxy( x[j], y[j] );
      cout << brik;



    }





  }






  // FlushConsoleInputBuffer( hInput );



  return 0;
}




////////////////////////////////////////////////////////////////////////////////

void spilleplade()
{



  //starter øverste grænse

  for ( int i = 1; i <= SCREEN_WIDTH; i++ )
  {
    gotoxy( i, 1 );
    cout << "*";
  }

  //starter venstre grænse

  for ( int j = 1; j <= SCREEN_HEIGHT; j++ )
  {
    gotoxy( 1, j );
    cout << "*";
  }

  //starter højre grænse

  for ( int h = 1; h <= SCREEN_HEIGHT; h++ )
  {
    gotoxy( 80, h );
    cout << "*";
  }

  //starter nedre grænse

  for ( int k = 1; k <= SCREEN_WIDTH; k++ )
  {

    gotoxy( k, SCREEN_HEIGHT );
    cout << "*";
  }



}

///////////////////////////////////////////////////////////////////////////////
void monster( int x, int y )
{


  char monster = -219; // opretter en variabel med en asciiværdi

  gotoxy( x, y );
  cout << monster;



}

///////////////////////////////////////////////////////////////////////////////
void Tjek_bane( Spiller Spiller )
{

  if ( Spiller.Position.X >= SCREEN_WIDTH )
  {

    gotoxy( 24, 65 );
    cout << " DU ER DØD ";
  }

  else if ( Spiller.Position.X <= 0 )
  {

    gotoxy( 24, 65 );
    cout << " DU ER DØD ";

  }
  else if ( Spiller.Position.Y >= SCREEN_HEIGHT - 1 )
  {

    gotoxy( 24, 65 );
    cout << " DU ER DØD ";
  }

  else if ( Spiller.Position.Y <= 0 )
  {
    gotoxy( 24, 65 );
    cout << " DU ER DØD ";


  }





}

///////////////////////////////////////////////////////////////////////////////
void Move_spiller( int dir, vector<int>x, vector<int>y, Spiller & Spiller )
{
  if ( dir == 2 ) // hvis retningen er op ad
  {
    Spiller.Position.Y--;
    y.push_back( Spiller.Position.Y );
    x.push_back( Spiller.Position.X );
  }
  else if ( dir == 2 ) // hvis retningen er til højre
  {
    Spiller.Position.X++;
    y.push_back( Spiller.Position.Y );
    x.push_back( Spiller.Position.X );
  }
  else if ( dir == 3 ) // hvis retningen er ned ad
  {
    Spiller.Position.Y++;
    y.push_back( Spiller.Position.Y );
    x.push_back( Spiller.Position.X );
  }
  else if ( dir == 2 ) // hvis retningen er til venstre
  {
    Spiller.Position.X--;
    y.push_back( Spiller.Position.Y );
    x.push_back( Spiller.Position.X );
  }







}


///////////////////////////////////////////////////////////////

jeg får compiler fejl:

bcc32 -D_DEBUG -g100 -j25 -Od -r- -k -y -v -vi- -tWC -c -IC:\CBuilderX\include -o"C:\Documents and Settings\dannie\cbproject\snake\windows\Debug_Build\File2.obj"  File2.cpp
Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland
File2.cpp:
"File2.cpp": E2227 Extra parameter in call to Move_spiller(Spiller &) in function main() at line 86
"File2.cpp": W8004 'dir' is assigned a value that is never used in function main() at line 135
"File2.cpp": W8066 Unreachable code in function main() at line 146
*** 1 errors in Compile ***
BCC32 exited with error code: 1
Build cancelled due to errors


hvad gør jeg galt???
Avatar billede ultragames Nybegynder
13. april 2004 - 22:30 #1
HAHA!.. ahr fundet fejlen!... havde ikke de andre variable med i prototypen :P
Avatar billede jpk Nybegynder
14. april 2004 - 07:58 #2
Du kunne evt. overveje at lave din Tjek_bane funktion sådan:

void Tjek_bane(const Spiller& Spiller)
{
  if(Spiller.Position.X >= SCREEN_WIDTH || Spiller.Position.X <= 0 || Spiller.Position.Y >= SCREEN_HEIGHT - 1 || Spiller.Position.Y <= 0)
  {
      gotoxy( 24, 65 );
      cout << " DU ER DØD ";
  }
}
Avatar billede jpk Nybegynder
14. april 2004 - 08:03 #3
char monster = -219; er ikke gyldig!
En char kan indeholde værdier fra -128 til 127
Avatar billede the_bma_man Nybegynder
20. juli 2004 - 13:12 #4
Hvorfor har du så ikke lukket spørgsmålet?!!!?
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester