Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Zapisywanie Wyniku gry do pliku txt.

Ostatnio zmodyfikowano 2016-01-21 00:21
Autor Wiadomość
damian662
Temat założony przez niniejszego użytkownika
Zapisywanie Wyniku gry do pliku txt.
» 2016-01-20 09:40:33
Witam ,


mam problem z zapisaniem wyniku ( punktów) do pliku txt sama gra działa prawidłowo lecz wynik który się zapisuje jest nieprawidłowy . Po zapisaniu wynik ma 7 cyfr np . 2292916 a powinien mieć 2 np .10 .


Więc coś jest nie tak a do końca nie wiem , także proszę o wsparcie . 



C/C++
#include <windows.h>
#define GLUT_DISABLE_ATEXIT_HACK
#include <gl/glut.h>
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;

using std::cout;
using std::endl;
using std::ostringstream;

#include "Sprite.h"
#include "Bridge.h"
#include "Enemy.h"
#include "Player.h"
#include "Bunker.h"
#include "Bullet.h"
#include "ComponentManager.h"
#include "ResourceFactory.h"
#include "Terrain.h"
#include "SpriteText.h"

int screenWidth = 800, screenHeight = 600;
int timeOut = GetTickCount() + 3000;

int currentLevel = 1;
const int maxLevel = 1;

enum GameState
{
    PLAY, GAMEOVER, YOUWON, LEVELSTART, LEVELFINISHED
} gameState = LEVELSTART;

Sprite * gameOver;
Sprite * youWon;
SpriteText * levelStart;
SpriteText * textHp;

SpriteText * textPoints;

void end( void )
{
    cout << "Game terminated" << endl;
}

void renderScene( void ) {
    glClearColor(.5f,.5f,.5f,.5f );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glLoadIdentity();
    glTranslatef( 0.f, 0.f, - 1.f );
    glPushMatrix();
    Terrain::get().draw();
    ComponentManager::get().draw();
    glPopMatrix();
    switch( gameState )
    {
    case PLAY:
        break;
    case GAMEOVER:
        gameOver->draw( float2( 0, 0 ), 0.f, 16.f );
        break;
    case YOUWON:
        youWon->draw( float2( 0, 0 ), 0.f, 16.f );
        break;
    case LEVELSTART:
        levelStart->draw( float2( - 9.f, 0.f ), 0, 2.5f );
        break;
    }
    textPoints->draw( float2( - 10.f, 7.f ), 0,.8f );
    textHp->draw( float2( - 10.f, - 7.f ), 0, 1.1f );
    glutSwapBuffers();
}

void resize( int wid, int hei )
{
    screenWidth = wid;
    screenHeight = hei ? hei: 1;
    glViewport( 0, 0, screenWidth, screenHeight );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    const float scale = 8.f; //9.5f+
    glOrtho( - scale *( float ) wid /( float ) hei *.98f, scale *( float ) wid /( float ) hei *.98f, - scale, scale,.1f, 10.f );
    glMatrixMode( GL_MODELVIEW );
}




void zapisz()
{
   
   
    ComponentManager::get().update();
    ComponentManager::get().find < Player >();
    Player * p = ComponentManager::get().next < Player >();
    SpriteText * textPoints;
    Player * potions;
   
   
    FILE * save = fopen( "D:\\wynik.txt", "w" );
    fprintf( save, "%i\n", potions );
    fclose( save );
}





void idle( void )
{
    switch( gameState )
    {
    case PLAY:
        {
            ComponentManager::get().update();
            ComponentManager::get().find < Player >();
            Player * p = ComponentManager::get().next < Player >();
            Player * potions();
            Player * getPoints();
            if( p != NULL ) // killed
           
           
            {
                ComponentManager::get().update();
                if( ComponentManager::get().count < Bridge >() == 0 )
                     gameState = LEVELFINISHED;
               
                timeOut = GetTickCount() + 4000;
                textPoints->setText( "Points: ", p->getPoints() );
               
                textHp->setText( "HP: ", p->getHp() );
            }
            else
            {
               
                ComponentManager::get().clear();
                gameState = GAMEOVER;
                timeOut = GetTickCount() + 4000;
            }
        }
        break;
    case GAMEOVER:
        if( GetTickCount() > timeOut )
        {
            ComponentManager::get().clear();
            Terrain::get().create( "level1.txt" );
            ComponentManager::get().find < Player >();
            Player * player = ComponentManager::get().next < Player >();
            if( player != NULL )
                 Terrain::get().setViewPos( player->getPosition() );
           
            gameState = LEVELSTART;
            currentLevel = 1;
            levelStart->setText( "Level ", currentLevel );
            timeOut = GetTickCount() + 3000;
        }
        break;
    case YOUWON:
        if( GetTickCount() > timeOut )
        {
           
           
           
            ComponentManager::get().update();
            ComponentManager::get().find < Player >();
            Player * p = ComponentManager::get().next < Player >();
            Player * getpotions;
            ComponentManager::get().clear();
            zapisz();
           
           
            Terrain::get().create( "level1.txt" );
            ComponentManager::get().find < Player >();
            Player * player = ComponentManager::get().next < Player >();
            if( player != NULL )
                 Terrain::get().setViewPos( player->getPosition() );
           
            gameState = LEVELSTART;
            currentLevel = 1;
            levelStart->setText( "Level ", currentLevel );
            timeOut = GetTickCount() + 3000;
        }
        break;
    case LEVELSTART:
        if( GetTickCount() > timeOut )
        {
            gameState = PLAY;
        }
        break;
    case LEVELFINISHED:
        ++currentLevel;
        if( currentLevel > maxLevel )
        {
           
           
           
            gameState = YOUWON;
            timeOut = GetTickCount() + 4000;
           
            currentLevel = 1;
            levelStart->setText( "Level ", currentLevel );
           
           
        }
        else
        {
            ComponentManager::get().clear();
            ostringstream o;
            o << "level" << currentLevel << ".txt";
            Terrain::get().create( o.str().c_str() );
            levelStart->setText( "Level ", currentLevel );
            timeOut = GetTickCount() + 3000;
            gameState = LEVELSTART;
            ComponentManager::get().find < Player >();
            Player * player = ComponentManager::get().next < Player >();
            if( player != NULL )
                 Terrain::get().setViewPos( player->getPosition() );
           
           
        }
        break;
    }
    glutPostRedisplay();
}

void keyb( unsigned char k, int x, int y )
{
    switch( k )
    {
    case 27:
        cout << "Shutdown by Escape" << endl;
        exit( EXIT_SUCCESS );
    }
    Player * p;
    ComponentManager::get().find < Player >();
    p = ComponentManager::get().next < Player >();
    if( p != NULL )
         p->keyb( k, true );
   
}

void keybUp( unsigned char k, int x, int y )
{
    Player * p;
    ComponentManager::get().find < Player >();
    p = ComponentManager::get().next < Player >();
    if( p != NULL )
         p->keyb( k, false );
   
}

int main( int argc, char ** argv ) {
    cout << "Initialization" << endl;
    glutInit( & argc, argv );
    glutInitDisplayMode( GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA );
    glutInitWindowPosition( 224, 64 );
    glutInitWindowSize( screenWidth, screenHeight );
    //    glutGameModeString("800x600:32");
    //    glutEnterGameMode();
    glutCreateWindow( "Tanks Game" );
    glutDisplayFunc( renderScene );
    glutReshapeFunc( resize );
    glutIdleFunc( idle );
    glutKeyboardFunc( keyb );
    glutKeyboardUpFunc( keybUp );
   
    glEnable( GL_BLEND );
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
    glEnable( GL_TEXTURE_2D );
   
    Terrain::get().create( "level1.txt" );
    Terrain::get().setViewSize( float2( 8, 16 ) );
    Terrain::get().setViewPos( float2( 10.5f, 25.f ) );
   
    gameOver = new Sprite( ResourceFactory::get().load( "gameover.bmp" ) );
    youWon = new Sprite( ResourceFactory::get().load( "youwon.bmp" ) );
   
    textPoints = new SpriteText( Sprite( ResourceFactory::get().load( "font.bmp" ), 128 ) );
    textPoints->setText( "Points: ", 0 );
    textPoints->setColor( 0, 1,.2f );
    textHp = new SpriteText( Sprite( ResourceFactory::get().load( "font.bmp" ), 128 ) );
    textHp->setText( "HP: ", 0 );
    textHp->setColor( 1, 0, 0 );
   
    levelStart = new SpriteText( Sprite( ResourceFactory::get().load( "font.bmp" ), 128 ) );
    levelStart->setText( "Level 1" );
    levelStart->setColor( 1.f, 1.f, 0.f );
   
   
   
   
   
   
    atexit( end );
   
    cout << "Game started" << endl;
   
    glutMainLoop();
}


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

#ifndef PLAYER_H
#define PLAYER_H

#include "Tank.h"

class Player
    : public Entity
{
public:
    Player( const Sprite &, float2, float );
    virtual ~Player();
   
    void update();
    void zapisz();
   
    void keyb( unsigned char, bool );
   
    int getPoints() { return points; }
    int getHp() { return hp; }
    void addPoints( int i ) { points += i; }
    int points;
   
protected:
    bool forward, backward, left, right;
   
private:
};

#endif // PLAYER_H
P-143805
pekfos
» 2016-01-21 00:21:16
C/C++
Player * potions;


FILE * save = fopen( "D:\\wynik.txt", "w" );
fprintf( save, "%i\n", potions );
Zapisujesz wartość niezainicjalizowanego wskaźnika.
P-143842
« 1 »
  Strona 1 z 1