日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

弹砖块游戏

發(fā)布時(shí)間:2023/12/20 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 弹砖块游戏 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

c語(yǔ)言實(shí)現(xiàn)彈磚塊游戲
利用板子使球不落到地上并且砸到磚塊上
windows平臺(tái)實(shí)現(xiàn)

代碼如下:

game.h:

#pragma once #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <conio.h>void HideCursor(); void gotoxy(int x, int y); void DataInit(); void Show(); void UpdateWithoutInput(); void UpdateWithInput();

test.cpp:

#include "game.h"int high; int width; int ball_v_x; int ball_v_y; int ball_x; int ball_y; int bricks_x; int bricks_y; int board_x; int board_y; int board_r; int score;int main() {DataInit(); //數(shù)據(jù)初始化while (1){Show(); //顯示畫(huà)面UpdateWithoutInput(); //與用戶(hù)無(wú)關(guān)的數(shù)據(jù)更新UpdateWithInput(); //與用戶(hù)有關(guān)的數(shù)據(jù)更新}return 0; }

game.cpp:

#include "game.h"extern int high; extern int width; extern int ball_v_x; extern int ball_v_y; extern int ball_x; extern int ball_y; extern int bricks_x; extern int bricks_y; extern int board_x; extern int board_y; extern int board_r; extern int score;void DataInit() {high = 20;width = 30;ball_x = 1;ball_y = width / 2;ball_v_x = 1;ball_v_y = 1;board_x = high - 1;board_y = width / 2;board_r = 5;bricks_x = 0;bricks_y = width / 2;score = 0;HideCursor(); }void Show() {gotoxy(0, 0);int i, j;for (i = 0; i <= high; i++){for (j = 0; j <= width; j++){if (j >= board_y - board_r && j <= board_y + board_r && i == board_x)printf("#");else if (i == bricks_x && j == bricks_y)printf("$");else if (i == ball_x && j == ball_y)printf("o");else if (i == high)printf("_");else if (j == width)printf("|");elseprintf(" ");}printf("\n");}printf("SCORE: %d\n", score);Sleep(100); }void UpdateWithoutInput() {if (ball_x <= 0 || ((ball_x == board_x - 1) && (ball_y >= board_y - board_r && ball_y <= board_y + board_r)))ball_v_x *= -1;if (ball_x > high){printf("wasted!!!\n");exit(-1);}ball_x += ball_v_x;if (ball_y >= width || ball_y <= 0)ball_v_y *= -1;ball_y += ball_v_y;if (ball_x == bricks_x && ball_y == bricks_y){score++;bricks_x = 0;bricks_y = rand();} }void UpdateWithInput() {char input;if (_kbhit()){input = _getch();if (input == 'a' && board_y > 1)board_y--;if (input == 'd' && board_y < width - 1)board_y++;} }

fix.cpp:
用于修復(fù)閃屏的問(wèn)題,goto函數(shù)可以用cls替代,但是會(huì)閃屏,本質(zhì)上都是用來(lái)清理屏幕的

#include "game.h" void HideCursor() {CONSOLE_CURSOR_INFO cursor_info = { 1,0 };SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); }void gotoxy(int x, int y) {HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);COORD pos;pos.X = x;pos.Y = y;SetConsoleCursorPosition(handle, pos); }

總結(jié)

以上是生活随笔為你收集整理的弹砖块游戏的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。