A game company's interview item (on)

xiaoxiao2021-04-10  371

The original title is as follows -

Explain: Develop a small procedure requirement: 1. Reflect your true level 2. There are design documents and annotations, as well as development cycle and cycling cycle 3. Illegal operations 4 will not occur under any operation. No resource leaks 5. Regular coding format: such as four grid 6. Use C (preferably using Microsoft's development tools) 7. Use object-oriented or updated model development 8. If you use architecture mode, design mode, usual, please mark in your code or document 9. Detached code, easy to read, easy to modify and expand

(Please do not use the ready-made framework such as MFC, ATL, WTL, using the Win32 API, but use STL, Boost and other C universal libraries will increase score, but the premise is to use the correct) program development step: 1. Establish a most basic Window program 2. Create a window, the window title name is "written test", the window style is WS_OVERLAPPEDWINDOW, the customer area size is 800 * 600, and the message loop 3 is established. Modifying the message loop can use the IDLE time (CPU idle time), please do not use Timer. 4. A picture "BG.BMP" is displayed on the window client area (0, 0) (the drawing can be used in GDI, or other libraries, such as the wind soul) 5. Press the number key 1, will load a picture in "spritex.bmp", the image will start the horizontal shift in a random position in the customer area, but will not fly out of the customer area, which means that it will be window Side frame bounce back. 6. Press Number Key 2, you will load a picture in "spritex.bmp", which starts up and down movement in a random position in the customer area, but will not fly out of the customer area, which means that it will be window Side frame bounce back. 7. Press Number Key 3, which will be loaded with a picture in "spritex.bmp", which will start working in a random direction in a random direction in a random direction, but not flying out of the customer area, that is Say that it will be bounced back by the window. 8. Use the left mouse button, if you click on the picture of the flight, then pop up a dialog, display the name of the clicked image (note that the picture of the display name must be flying on the top layer) 9. Press the right mouse button, all flight pictures are moving down to the mouse, and when the mouse moves, the movement will be followed, but still cannot move the outbound frame. Right click on the mouse, all the images continue to move according to their original rules, such as the image created by the number button 1, then continue to work horizontally, such as the image created by the numeric key 2, then continue Make up and down.

Note: The efficiency of the operation of the program is secondary, the main investigation point is the habit of the structure and development of the program.

We offer: sample maps running, program resources needed to use

- currently only makes a single effect

// Terminal file

#pragma overce

Class game {private: int Num, VX, Vy; long x, y; hbitmap bmp, map; char filename [20];

HDC HDC, MDC, BUFDC; Rect Rect; Public: Game (Void); Public: ~ Game (Void); Public: Void Init (HDC SHDC, Rect SRect); Void Move (); Void Move (long mx, long my INT flag;}; // realize file

#include "stdafx.h" #include "game.h" #include "time.h" #include "stdio.h"

Game :: Game (void) {

}

Game :: ~ Game (void) {}

Void game :: init (HDC SHDC, Rect SREC) {HDC = SHDC; MDC = CreateCompatibleDC (HDC); BUFDC = CreateCompaTibleDC (HDC); Rect = SRECT; SRAND ((unsigned); vx = Vy = 20; x = rand ()% 750; y = rand ()% 550; Num = rand ()% 3; sprintf (filename, "" sprite% d.bmp ", num); bmp = (hbitmap) loadImage (null, FileName, Image_bitmap, 32, 47, Lr_LoadFromFile; Map = (Hbitmap) loadImage (NULL, _T ("bg.bmp"), image_bitmap, 800, 600, lr_loadfromfile;}

Void Game :: Move () {SelectObject (MDC, Map); Bitblt (HDC, 0, 0, 800, 600, MDC, 0, 0, Srcopy); SelectObject (MDC, BMP); Bitblt (HDC, X, Y, 32, 47 , MDC, 0, 0, SRCCPY;

Switch (flag) {casse 1:

X = vx; if (x> = (Rect.right-32)) {x = Rect.right-32; vx = -VX;} else if (x <= 0) {x = 0; vx = -VX; } Break;

Case 2: /

Y = Vy; if (y> = (Rect.bottom-47)) {y = Rect.bottom-47; Vy = -vy;} else if (y <= 0) {y = 0; VY = -vy; } Break;

Case 3:

INT k = rand ()% 2; if (k == 0) k = -1; INT j = rand ()% 2; if (j == 0) j = -1; // Random direction determination

X = (vx / k); y = (VY / J);

IF (x> = (Rect.right-32)) {x = Rect.right-32; vx = -VX;} else if (x <= 0) {x = 0; vx = -VX;} if (Y > = (Rect.Bottom-47)) {y = Rect.bottom-47; Vy = -vy;} else if (y <= 0) {y = 0; VY = -Vy;} Break;}} // main

// The written test.cpp: Defines the application's entry point. //

#include "stdafx.h" #include "written test .h" #include "game.h"

#define max_loadstring 100

// Global variable: hinstance hinst; // Current instance tchar sztitle [max_loadstring]; // Tit bar text tchar szwindowclass [max_loadstring]; // Main window class name

DWORD TNOW, TPRE; // Animation Circulation HDC HDC; Rect Rect; Void MyPaint (); // Draw Function Game * Game = New Game; Game * Game2 = New Game;

The forward declaration of the function contained in this code module: atom myregisterclass (hinstance hinstance); Bool InitInstance (Hinstance, Int); LRESULT CALLBACK WNDPROC (HWND, UINT, WPARAM, LPARAM);

Int apientry _twinmain (Hinstance Hinstance, Hinstance Hprevinstance, LPTSTSTR LPCMDLINE, INT NCMDSHOW) {unreference; unreferenced_parameter (LPCMDLINE);

// Todo: Place the code here. Haccel HaccelTable;

// Initialize the global string LoadString (Hinstance, IDs_App_title, sztitle, max_loadstring); LoadString (Hinstance, IDC_MY, SzWindowClass, Max_LoadString); MyRegisterClass (Hinstance);

// Perform an application initialization: if (! Initinstance (hinstance (ncmdshow)) {Return False;}

HaccelTable = loading (Hinstance, MakeintResource (IDC_MY));

// Main message loop: msg msg; ZeromeMory (& MSG, SIZEOF (MSG)); While (Msg.Message! = WM_QUIT) {IF (PEEKMESG, NULL, 0, 0, PM_Remove) {TranslateMessage (& MSG); DispatchMessage (& MSG);} else {t1 = gettickcount (); if (TNOW-TPRE> = 400) mypaint ();}} return (int) msg.wparam;}

// // Function: myregisterclass () // // Purpose: Register window classes. // // Note: // The function is only required when you want to // this code is compatible with Win32 system before the "RegisterClassex" // function added to Windows 95. Calling this function is very important, // This application can get the associated // "formatted" small icon. // 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 (hInstance, MAKEINTRESOURCE (IDI_MY)); wcex.hCursor = LoadCursor (NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH) (COLOR_WINDOW 1); wcex.lpszMenuName = NULL; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon (wcex.hInstance, MAKEINTRESOURCE (IDI_SMALL));

Return RegisterClassex (& WCEX);

// // Function: InitInstance (Hinstance, Int) // // Purpose: Save instance handles and create main windows // // Note: // In this function, we save instant handles in global variables and / / Create and display the main program window. // Bool InitInstance (Hinstance Hinstance, Int ncmdshow) {hwnd hwnd;

Hinst = hinstance; // Store an instance handle in the global variable

HWnd = CreateWindow (SzwindowClass, Sztitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, HINSTANCE, NULL);

IF (! hwnd) {Return False;}

MoveWindow (HWND, 22, 22, 800, 600, 1); showwindow (hwnd, ncmdshow); UpdateWindow (HWND);

GetClientRect (hwnd, & review); // ---------------- Rect HDC = getDC (HWND); Game-> Init (HDC, RECT); Game-> Flag = 1 ; // game2-> init (hdc, rect); // Game-> flag = 1;

Return True;}

// // Function: WndProc (HWND, UINT, WPARAM, LPARAM) // // Purpose: Handle the message of the main window. // // WM_COMMAND - Processing Application Menu // WM_Paint - Drawing Main Window // WM_DESTROY - Send Exit Messages and Returns /// Lresult Callback WndProc (HWND HWND, UINT Message, WParam WParam, LParam Lparam) {Switch (Message ) {Case WM_DESTROY: PostquitMessage (0); Break; Default: Return DefWindowProc (HWND, Message, WPARAM, LPARAM);} Return 0;

Void mypaint () {game-> move (); game2-> move (); tpre = gettickcount ();

The first write is only to realize the function.

No problem, considering structured

Later, I found a pair of pictures to be added each time the button.

Start considering the class to achieve

I thought it was very good after packaging.

But I found it only to move one

I don't know how to modify it.

Asked in 9CBS

Master wants to save background -> Local Drawing -> Delay -> Recovery Background -> Calculate New Location and Save Background -> ....

I thought about it or not.

I want to search online but I can't find it.

Depressed, is there anyone guidance?

转载请注明原文地址:https://www.9cbs.com/read-133264.html

New Post(0)