Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 631687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:59:36+00:00 2026-05-13T19:59:36+00:00

I am totally new in programming. For my first programm I tried to make

  • 0

I am totally new in programming.
For my first programm I tried to make a small game in c with windows api.
the following code is a nearly working snake, (some bugs not fixed yet)
but i cant find a working solution to fix that flickering.
(I dont know double Buffering so I just took from here: [http://www.codeguru.com/forum/archive/index.php/t-272723.html%5D%5B1%5D

// snake1.02.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "snake1.02.h"
#include "stdio.h"

#define ID_MYTIMER 1234567
#define ID_BERRY 1
#define SEGMENTSIZE 10

const WCHAR szClassName[] = L"Snake1.02";
HWND hWnd;
UINT myTimer;

struct point {
    int x, y;
};



point snake[500];



int dx = SEGMENTSIZE;  //Change direction l=l+1
int dy = 0;
int snakelength=3;
int highscore=0;
WCHAR nachricht[100];
int map[80][60]; //Wenn Bildschirmgröße angepasst wird, auch hier anpassen!!!



// Global Variables:
HINSTANCE hInst;                                // current instance

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.
    MSG msg;

    // Initialize global strings
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    myTimer = SetTimer ( hWnd, ID_MYTIMER, 40, NULL );

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    KillTimer ( hWnd, myTimer );
    return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+2);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szClassName;
    wcex.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);

    return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{


   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szClassName, szClassName, WS_OVERLAPPED,
      CW_USEDEFAULT, 0, 800, 600, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   for(int i=snakelength; i>=0; i--){
   snake[i].x =100-(i*SEGMENTSIZE);
   snake[i].y=20;}

   for(int x=0; x<78;x++){
   map[x][0]=1;
   map[x][56]=1;


   }

   for(int y=0; y<57;y++){
    map[0][y]=1;
    map[78][y]=1;

   }


    map[50][70]=2;





   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    HDC memDC;
    HBITMAP hMemBmp;
    HBITMAP hOldBmp;


    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        memDC = CreateCompatibleDC(hdc);                                //flimmerfrei
        hMemBmp = CreateCompatibleBitmap(hdc, 800, 600);
        hOldBmp = (HBITMAP)SelectObject(memDC, hMemBmp);
        // TODO: Add any drawing code here...

        for (int i = 0; i<=snakelength; i++) {
            Rectangle(memDC, snake[i].x,snake[i].y,snake[i].x+10,snake[i].y+10);
        }
        for (int y=0; y<60;y++){
        for (int x=0; x<80;x++){
            if(map[x][y]==1||map[x][y]==2){
        Rectangle(memDC,x*SEGMENTSIZE,y*SEGMENTSIZE,x*SEGMENTSIZE+SEGMENTSIZE,y*SEGMENTSIZE+SEGMENTSIZE);
            }}
        }
        BitBlt(hdc, 0, 0, 800, 600, memDC, 0, 0, SRCCOPY);


        SelectObject(memDC, hOldBmp);
        DeleteObject(hMemBmp);
        DeleteDC(memDC);
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    case WM_TIMER:
        switch (wParam)
        {
        case ID_MYTIMER:  {
            if (map[snake[0].x/10][snake[0].y/10]==1){              //crashcheck
                KillTimer ( hWnd, myTimer );
                wsprintf (nachricht,L"Game Over. Punktestand: %d",highscore);
                MessageBox(hWnd, (LPCWSTR)nachricht,L"Ende",0);
                }
            if (map[snake[0].x/10][snake[0].y/10]==2)               //grow
            {               
                for (int i = snakelength; i>0; i--) 
                {
                    snake[i].x = snake[i-1].x;
                    snake[i].y = snake[i-1].y;
                }
                snake[0].x = snake[1].x + dx;
                snake[0].y = snake[1].y + dy;
                map[snake[0].x/10][snake[0].y/10]=0;
                snakelength++;
                highscore+=10;

            }

            for (int i = snakelength; i>0; i--) {
                snake[i].x = snake[i-1].x;
                snake[i].y = snake[i-1].y;
            }
                snake[0].x = snake[1].x + dx;
                snake[0].y = snake[1].y + dy;   



            InvalidateRect(hWnd, 0,1);
            return 0;
            }
                          }
        break;
    case WM_KEYDOWN: {
        switch (wParam) {


            case VK_DOWN:{
                if(dy!=-SEGMENTSIZE){
                dy=SEGMENTSIZE;
                dx=0;
                }
                  }
            break;


            case VK_RIGHT:{
                if (dx!=-SEGMENTSIZE){
                dy=0;
                dx=SEGMENTSIZE;
                }
                  }
            break;
            case VK_UP:{
                if (dy!=SEGMENTSIZE){
                dy=-SEGMENTSIZE;
                dx=0;
                }
                  }
            break;
            case VK_LEFT:{
                if(dx!=SEGMENTSIZE){
                dy=0;
                dx=-SEGMENTSIZE;
                }
                }
                break;
            case VK_ESCAPE:{
                ExitProcess(0);
                  }
            break;
        }
        }
     break;

    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:59:36+00:00Added an answer on May 13, 2026 at 7:59 pm

    Add one case to your main switch statement in WndProc:

    case WM_ERASEBKGND: 
        return TRUE;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.