dummy
Legacy Member
Onze "opdracht" voor deze week (in DAE) was een statisch spel te programmeren. Ik heb een OXO spel gemaakt, maar er is een probleem:
-eerst geeft ge de kleuren in van het speelveld - OK
-dan worden buttons ge-enabled (X, O en score) - OK
-dan is het de bedoeling dat als er op X geklickt wordt en dan op het speelveld, op die plaats een X verschijnt - NOK
-als ik dan op O klik, komt de O op de plaats waar ik daarnet geklickt had om een X te plaatsen. - NOK
-als ik dan weer op X klik, verandert de O op het veld terug in een X (een andere positie kan dus niet gekozen worden, of de voorgaande X of O gaat verloren) - NOK
dit is mijn code :
-eerst geeft ge de kleuren in van het speelveld - OK
-dan worden buttons ge-enabled (X, O en score) - OK
-dan is het de bedoeling dat als er op X geklickt wordt en dan op het speelveld, op die plaats een X verschijnt - NOK
-als ik dan op O klik, komt de O op de plaats waar ik daarnet geklickt had om een X te plaatsen. - NOK
-als ik dan weer op X klik, verandert de O op het veld terug in een X (een andere positie kan dus niet gekozen worden, of de voorgaande X of O gaat verloren) - NOK
dit is mijn code :
Code:
//-----------------------------------------------------------------
// Game File
// C++ Source - X.cpp - version 2006 v2_01
// Copyright Kevin Hoefman - [email protected]
// http://www.digitalartsandentertainment.be/
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "TestSpel_v01.h" // TODO: pas de naam X aan
//-----------------------------------------------------------------
// Defines
//-----------------------------------------------------------------
#define GAME_ENGINE (GameEngine::GetSingleton())
//-----------------------------------------------------------------
// TestSpel_v01 methods // TODO: pas de naam X aan
//-----------------------------------------------------------------
TestSpel_v01::TestSpel_v01() // TODO: pas de naam X aan (2 maal)
{
vakGrootte = 50;
iSpeler1 = 0;
iSpeler2 = 0;
iRood1 = iRood2 = iGroen1 = iGroen2 = iBlauw1 = iBlauw2 = 255;
}
TestSpel_v01::~TestSpel_v01() // TODO: pas de naam X aan (2 maal)
{
// nothing to destroy
}
// TODO: schrijf hier de implementatie van de functies die je uit commentaar gehaald hebt in de header file.
bool TestSpel_v01::CallAction (Caller *speler)
{
if(speler->GetType()==Caller::Button)
{
if(speler==btnX)
{
T = 1;
GAME_ENGINE->Repaint();
}
else if(speler==btnO)
{
T = 2;
GAME_ENGINE->Repaint();
}
else if(speler==btnSpeler1)
{
iSpeler1++;
GAME_ENGINE->Repaint();
return true;
}
else if(speler==btnSpeler2)
{
iSpeler2++;
GAME_ENGINE->Repaint();
return true;
}
else if(speler==btnReset)
{
iSpeler1 = 0;
iSpeler2 = 0;
GAME_ENGINE->Repaint();
return true;
}
else if(speler==btnTeken)
{
iRood1 = txtRood1->GetText().ToInteger();
iGroen1 = txtGroen1->GetText().ToInteger();
iBlauw1 = txtBlauw1->GetText().ToInteger();
iRood2 = txtRood2->GetText().ToInteger();
iGroen2 = txtGroen2->GetText().ToInteger();
iBlauw2 = txtBlauw2->GetText().ToInteger();
if (iRood1 >= 0 && iRood1 < 256 && iGroen1 >= 0 && iGroen1 < 256 && iBlauw1 >= 0 && iBlauw1 < 256 && iRood2 >= 0 && iRood2 < 256 && iGroen2 >= 0 && iGroen2 < 256 && iBlauw2 >= 0 && iBlauw2 < 256)
{
btnO->SetEnabled(true);
btnX->SetEnabled(true);
btnSpeler1->SetEnabled(true);
btnSpeler2->SetEnabled(true);
btnReset->SetEnabled(true);
GAME_ENGINE->Repaint();
return true;
}
else if(iRood1 < 0 || iRood1 >= 256 || iGroen1 < 0 || iGroen1 >= 256 || iBlauw1 < 0 || iBlauw1 >= 256 || iRood2 < 0 || iRood2 >= 256 || iGroen2 < 0 || iGroen2 >= 256 || iBlauw2 < 0 || iBlauw2 >= 256)
{
GAME_ENGINE->MessageBox("Verkeerde waarde ingevuld. \nVoer een waarde in tussen \n0 en 255.");
return true;
}
}
}
return true;
}
void TestSpel_v01::GameInitialize(HINSTANCE hInstance) // pas X aan
{
AbstractGame::GameInitialize(hInstance);
GAME_ENGINE->SetTitle("OXO ~ Written with Game Engine version 2_01 ~ (TestSpel_v01)"); // pas X aan
}
void TestSpel_v01::GameStart()
{
T = 0;
GAME_ENGINE->SetTitle("OXO ~ Written by dummy");
GAME_ENGINE->MessageBox("Kies de kleuren voor het veld\nen druk op 'Tekenen' om het\nspel te starten.");
btnO = new Button("O");
btnO->SetBounds(10, 10, 25, 25);
btnO->SetAction(this);
btnO->SetEnabled(false);
btnO->Show();
btnX = new Button("X");
btnX->SetBounds(10, 40, 25, 25);
btnX->SetAction(this);
btnX->SetEnabled(false);
btnX->Show();
btnSpeler1 = new Button("Speler 1");
btnSpeler1->SetBounds(10, 70, 75, 25);
btnSpeler1->SetAction(this);
btnSpeler1->SetEnabled(false);
btnSpeler1->Show();
btnSpeler2 = new Button("Speler 2");
btnSpeler2->SetBounds(10, 100, 75, 25);
btnSpeler2->SetAction(this);
btnSpeler2->SetEnabled(false);
btnSpeler2->Show();
btnTeken = new Button("Tekenen");
btnTeken->SetBounds(800, 150, 100, 25);
btnTeken->SetAction(this);
btnTeken->Show();
btnReset = new Button("Reset Score");
btnReset->SetBounds(10, 210, 125, 25);
btnReset->SetAction(this);
btnReset->SetEnabled(false);
btnReset->Show();
txtRood1 = new TextBox("255");
txtRood1->SetBounds(700, 50, 75, 20);
txtRood1->Show();
txtGroen1 = new TextBox("255");
txtGroen1->SetBounds(700, 75, 75, 20);
txtGroen1->Show();
txtBlauw1 = new TextBox("255");
txtBlauw1->SetBounds(700, 100, 75, 20);
txtBlauw1->Show();
txtRood2 = new TextBox("127");
txtRood2->SetBounds(920, 50, 75, 20);
txtRood2->Show();
txtGroen2 = new TextBox("127");
txtGroen2->SetBounds(920, 75, 75, 20);
txtGroen2->Show();
txtBlauw2 = new TextBox("127");
txtBlauw2->SetBounds(920, 100, 75, 20);
txtBlauw2->Show();
}
void TestSpel_v01::GameEnd()
{
delete btnO, btnX, btnSpeler1, btnSpeler2, btnTeken, txtRood1, txtRood2, txtGroen1, txtGroen2, txtBlauw1, txtBlauw2;
}
void TestSpel_v01::GamePaint(RECT rect)
{
//Selectie kleur
GAME_ENGINE->SetColor(RGB(0, 0, 0));
GAME_ENGINE->DrawString("Kleur 1", 700, 20);
GAME_ENGINE->DrawString("Rood", 625, 50);
GAME_ENGINE->DrawString("Groen", 625, 75);
GAME_ENGINE->DrawString("Blauw", 625, 100);
GAME_ENGINE->DrawString("Kleur 2", 920, 20);
GAME_ENGINE->DrawString("Rood", 850, 50);
GAME_ENGINE->DrawString("Groen", 850, 75);
GAME_ENGINE->DrawString("Blauw", 850, 100);
GAME_ENGINE->SetColor(RGB(iRood1, iGroen1, iBlauw1));
GAME_ENGINE->DrawString(String("(") + iRood1 + ", " + iGroen1 + ", " + iBlauw1 + ")", 600, 130);
GAME_ENGINE->SetColor(RGB(iRood2, iGroen2, iBlauw2));
GAME_ENGINE->DrawString(String("(") + iRood2 + ", " + iGroen2 + ", " + iBlauw2 + ")", 600, 160);
//Speelveld
RECT r;
r.left = 0;
r.right = GAME_ENGINE->GetWidth();
r.top = 0;
r.bottom = GAME_ENGINE->GetHeight();
TekenSpeelVeld(r);
//Teken X of O
if(T == 1)
{
GAME_ENGINE->SetColor(RGB(0, 0, 0));
GAME_ENGINE->DrawString(String("X"), temp.x, temp.y);
T = 0;
}
else if(T == 2)
{
GAME_ENGINE->SetColor(RGB(0, 0, 0));
GAME_ENGINE->DrawString(String("O"), temp.x, temp.y);
T = 0;
}
//Score
GAME_ENGINE->SetColor(RGB(255, 0, 0));
GAME_ENGINE->DrawString(String("Speler 1: ") + iSpeler1, 10, 150);
GAME_ENGINE->DrawString(String("Speler 2: ") + iSpeler2, 10, 180);
}
void TestSpel_v01::TekenSpeelVeld(RECT Grootte)
{
int xCenter = Grootte.right / 2;
int yCenter = Grootte.bottom / 2;
//teken rand
GAME_ENGINE->SetColor(RGB(0, 0, 0));
GAME_ENGINE->DrawRect(xCenter - 5 * vakGrootte - 1, yCenter - 5 * vakGrootte - 1, 10 * vakGrootte + 2, 10 * vakGrootte + 2);
GAME_ENGINE->DrawRect(xCenter - 5 * vakGrootte - 5, yCenter - 5 * vakGrootte - 5, 10 * vakGrootte + 10, 10 * vakGrootte + 10);
//teken onderste vlak in het lichtgrijs
GAME_ENGINE->SetColor(RGB(iRood1, iGroen1, iBlauw1));
GAME_ENGINE->FillRect(xCenter - 5 * vakGrootte, yCenter - 5 * vakGrootte, 10 * vakGrootte, 10 * vakGrootte);
//teken donkergrijze vlakken
GAME_ENGINE->SetColor(RGB(iRood2, iGroen2, iBlauw2));
for (int rijen = 0; rijen < 10; rijen++)
{
for (int kolommen = 0; kolommen < 10; kolommen++)
{
if ((rijen + kolommen) % 2 == 1)
{
GAME_ENGINE->FillRect(xCenter + (rijen - 5) * vakGrootte, yCenter + (kolommen - 5) * vakGrootte, vakGrootte, vakGrootte);
}
}
}
}
void TestSpel_v01::MouseButtonAction(bool isLeft, bool isDown, int x, int y, WPARAM wParam)
{
if (isLeft && isDown)
{
//over welk vak bevindt zich de muis???
temp.x = x;
temp.y = y;
GAME_ENGINE->Repaint();
}
}