void World::Update() { if(m_state == IDLE) { if(LoadResources()) { } } else if(m_state == PLAYING) { if(!IsBallInBounds()) { ChangeState(GAMEOVER); } else { UpdateBall(); UpdatePlayer1(); UpdatePlayer2(); } } else if(m_state == GAMEOVER) { if(HasBallCollideLeft()) { m_player2.UpdateScore(1); } else if(HasBallCollideRight()) { m_player1.UpdateScore(1); } Restart(); } }
void MouseProcess(void) { Msg msg = GetMessage(); if(msg == MSG_SWITCH){ CurrentProcess = KeyboardProcess; CurrentSystick = KeyboardSystick; KeyboardUIInit(); for(u32 i=0;i<sizeof(JoyMouseBuffer);i++){ JoyMouseBuffer[i] = 0; } JoyMouseBuffer[IDX_ID] = MOUSE_REPORT_ID; UserToPMABufferCopy(JoyMouseBuffer, GetEPTxAddr(ENDP2), 5); /* enable endpoint for transmission */ SetEPTxCount(ENDP2, 5); SetEPTxValid(ENDP2); }else if(KEY_PAUSE == msg){ bMode = !bMode; if(bMode){ AirMouseUIInit(); }else{ MouseUIInit(); } }else if(msg){ JoyMouseBuffer[IDX_ID] = MOUSE_REPORT_ID; UserToPMABufferCopy(JoyMouseBuffer, GetEPTxAddr(ENDP2), 5); /* enable endpoint for transmission */ SetEPTxCount(ENDP2, 5); SetEPTxValid(ENDP2); if(bMode){ UpdateBall(); } } }
/* ============================= idGameBustOutWindow::UpdateGame ============================= */ void idGameBustOutWindow::UpdateGame() { int i; if ( onNewGame ) { ResetGameState(); // Create Board SetCurrentBoard(); gamerunning = true; } if ( onContinue ) { gameOver = false; ballsRemaining = 3; onContinue = false; } if ( onNewLevel ) { currentLevel++; ClearBoard(); SetCurrentBoard(); ballSpeed = BALL_SPEED * ( 1.f + ((float)currentLevel/5.f) ); if ( ballSpeed > BALL_MAXSPEED ) { ballSpeed = BALL_MAXSPEED; } updateScore = true; onNewLevel = false; } if(gamerunning == true) { UpdatePaddle(); UpdateBall(); UpdatePowerups(); for( i = 0; i < entities.Num(); i++ ) { entities[i]->Update( timeSlice, gui->GetTime() ); } // Delete entities that need to be deleted for( i = entities.Num()-1; i >= 0; i-- ) { if( entities[i]->removed ) { BOEntity* ent = entities[i]; delete ent; entities.RemoveIndex(i); } } if ( updateScore ) { UpdateScore(); updateScore = false; } } }
UpdateBallz(ball * ballz){ ball * head = ballz; while (head){ UpdateBall(head); head = head->next; } }
// Update game (one frame) void UpdateGame(void) { if (!gameOver) { if (IsKeyPressed('P')) pause = !pause; if (!pause) { // Player movement if (IsKeyDown(KEY_LEFT)) player.position.x -= 5; if ((player.position.x - player.size.x/2) <= 0) player.position.x = player.size.x/2; if (IsKeyDown(KEY_RIGHT)) player.position.x += 5; if ((player.position.x + player.size.x/2) >= screenWidth) player.position.x = screenWidth - player.size.x/2; // Launch ball if (!ball.active) { if (IsKeyPressed(KEY_SPACE)) { ball.active = true; ball.speed = (Vector2){ 0, -5 }; } } UpdateBall(); // Game over logic if (player.life <= 0) gameOver = true; else { gameOver = true; for (int i = 0; i < LINES_OF_BRICKS; i++) { for (int j = 0; j < BRICKS_PER_LINE; j++) { if (brick[i][j].active) gameOver = false; } } } } } else { if (IsKeyPressed(KEY_ENTER)) { InitGame(); gameOver = false; } } }
// Updates functions and positions every frame // void UpdateGame(bool &play, DynamObject &redPaddle, DynamObject &bluePaddle, DynamObject &ball, int &redScore, int &blueScore, float &ballInitX, float &ballInitY, int &ballDir, bool &winLose, bool &quit) { if(winLose == false) { StartBall(play, ball, ballDir); BallCollisionWalls(ball, redScore, blueScore, ballInitX, ballInitY, play, ballDir); BallCollisionPaddles(ball, redPaddle, bluePaddle, play); PaddleMovement(redPaddle); AIBehaviour(bluePaddle, ball); UpdateBall(ball); } WinLose(redScore, blueScore, winLose, quit); }
void UpdateBallz (ball * ballz) { ball *head = ballz; while (head) { UpdateBall (head); head = head->next; } Collision (ballz); }
char PongGameLoop(void) { unsigned long dt; unsigned long thisTime; thisTime = GLIB_GetGameMillis(); dt = thisTime - g_lastTime; g_lastTime = thisTime; if (GetBitUInt8(&g_pongState, PONG_PLAYING)) { UpdateBall(&g_pongBall, dt); UpdatePlayerPaddle(&g_player, dt); UpdateCompPaddle(&g_comp, dt); HandleInput(); CheckForCollisions(); } RenderPong(dt); return 1; }
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_CREATE: hbmBall = LoadBitmap(g_hInst, "BALLBMP"); hbmMask = LoadBitmap(g_hInst, "MASKBMP"); if(!hbmBall || !hbmMask){ MessageBox(hwnd, "Load of resources failed.", "Error", MB_OK | MB_ICONEXCLAMATION); return -1; } GetObject(hbmBall, sizeof(bm), &bm); SetTimer(hwnd, idTimer1, nTimerDelay, NULL); ballX = 0; ballY = 0; deltaX = deltaValue; deltaY = deltaValue; break; case WM_TIMER: if(hbmBall && hbmMask) { HDC hdcWindow; hdcWindow = GetDC(hwnd); EraseBall(hdcWindow); UpdateBall(hwnd); DrawBall(hdcWindow); ReleaseDC(hwnd, hdcWindow); } break; case WM_PAINT: if(hbmBall && hbmMask) { PAINTSTRUCT ps; HDC hdcWindow; hdcWindow = BeginPaint(hwnd, &ps); DrawBall(hdcWindow); EndPaint(hwnd, &ps); } break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: KillTimer(hwnd, idTimer1); DeleteObject(hbmBall); DeleteObject(hbmMask); PostQuitMessage(0); break; default: return DefWindowProc(hwnd, Message, wParam, lParam); } return 0; }
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: { UINT ret; BITMAP bm; g_hbmBall = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL)); if(g_hbmBall == NULL) MessageBox(hwnd, "Could not load IDB_BALL!", "Error", MB_OK | MB_ICONEXCLAMATION); g_hbmMask = CreateBitmapMask(g_hbmBall, RGB(0, 0, 0)); if(g_hbmMask == NULL) MessageBox(hwnd, "Could not create mask!", "Error", MB_OK | MB_ICONEXCLAMATION); GetObject(g_hbmBall, sizeof(bm), &bm); ZeroMemory(&g_ballInfo, sizeof(g_ballInfo)); g_ballInfo.width = bm.bmWidth; g_ballInfo.height = bm.bmHeight; g_ballInfo.dx = BALL_MOVE_DELTA; g_ballInfo.dy = BALL_MOVE_DELTA; ret = SetTimer(hwnd, ID_TIMER, 50, NULL); if(ret == 0) MessageBox(hwnd, "Could not SetTimer()!", "Error", MB_OK | MB_ICONEXCLAMATION); } break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_PAINT: { RECT rcClient; PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rcClient); DrawBall(hdc, &rcClient); EndPaint(hwnd, &ps); } break; case WM_TIMER: { RECT rcClient; HDC hdc = GetDC(hwnd); GetClientRect(hwnd, &rcClient); UpdateBall(&rcClient); DrawBall(hdc, &rcClient); ReleaseDC(hwnd, hdc); } break; case WM_DESTROY: KillTimer(hwnd, ID_TIMER); DeleteObject(g_hbmBall); DeleteObject(g_hbmMask); PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; }
void RunGame() { UpdatePlayer(); UpdateAI(); UpdateBall(); }