跳到正文

个人记录

奇奇怪怪的贪吃蛇解析

Zhoumy约 6 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 6 分钟

版本

  1. v1当前

奇怪的知识增加了呢,本故事纯属虚构…还是这里 既然有人要源码,那就给吧(主函数) 另两个小窗口的代码就自己想吧,应该都会自己写。

C++
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<stdio.h>
#include<queue>
#include<iomanip>
#include<string>
#include<cstdio>
#include<time.h>
#include<Windows.h>
#include<conio.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
#define random(a,b) (rand()%(b-a)+a)
using namespace std;
typedef BOOL (WINAPI *PROCSETCONSOLEFONT)(HANDLE, DWORD);
PROCSETCONSOLEFONT SetConsoleFont;
POINT pt;
HWND hwnd[50],red;
int doa=1;
int playerXmode=1,playerYmode=1;
struct Ball{
	float x,y;
}ball[100000];
struct what{
	float x,y,dx,dy;
}player;
void makething(int id,int x,int y){
	RECT rect;
	GetWindowRect(hwnd[id],&rect);
	MoveWindow(hwnd[id],x,y,rect.right-rect.left,rect.bottom-rect.top,TRUE);
}
void redd(int x,int y){
	RECT rect;
	GetWindowRect(red,&rect);
	MoveWindow(red,x,y,rect.right-rect.left,rect.bottom-rect.top,TRUE);
}
int main(){
	srand(time(NULL));
	player.x=random(0,1100);
	player.y=random(0,610);
	player.dx=5;
	player.dy=5;
	system("mode con cols=16 lines=8");
	hwnd[0]=GetForegroundWindow();
	makething(0,0,-200);
	for(int i=1;i<=15;i++){
		system("start white.exe");
		Sleep(30);
		hwnd[i]=GetForegroundWindow();
		makething(i,-90*i,-200);
	}
	system("start red.exe");
	Sleep(100);
	red=GetForegroundWindow();
	int j=0,i=0;
	for(;;){
		if(KEY_DOWN('W')){
			playerYmode=-1;
			if(KEY_DOWN('A'))playerXmode=1;
			else if(KEY_DOWN('D'))playerXmode=-1;
			else playerXmode=0;
		}
		if(KEY_DOWN('S')){
			playerYmode=1;
			if(KEY_DOWN('A'))playerXmode=1;
			else if(KEY_DOWN('D'))playerXmode=-1;
			else playerXmode=0;
		}
		if(KEY_DOWN('A')){
			playerXmode=-1;
			if(KEY_DOWN('W'))playerYmode=-1;
			else if(KEY_DOWN('S'))playerYmode=1;
			else playerYmode=0;
		}
		if(KEY_DOWN('D')){
			playerXmode=1;
			if(KEY_DOWN('W'))playerYmode=-1;
			else if(KEY_DOWN('S'))playerYmode=1;
			else playerYmode=0;
		}
		if(playerXmode==1)ball[0].x+=50;
		if(playerXmode==-1)ball[0].x-=50;
		if(playerYmode==1)ball[0].y+=50;
		if(playerYmode==-1)ball[0].y-=50;
		if(ball[1].x<0)ball[0].x=1140;
		if(ball[1].x>1141)ball[0].x=0;
		if(ball[1].y<0)ball[0].y=650;
		if(ball[1].y>651)ball[0].y=0;
		redd(player.x,player.y);
		if(player.x<ball[0].x&&player.x>ball[0].x-130&&player.y<ball[0].y&&player.y>ball[0].y-130){
			srand(time(0));
			player.y=random(0,610);
			player.x=random(0,1100);
			doa++;
		}
		for(int i=doa;i>=1;i--){
			makething(i,ball[i].x,ball[i].y);
			ball[i].x=ball[i-1].x;
			ball[i].y=ball[i-1].y;
		}
		player.x+=player.dx;
		player.y+=player.dy;
		if(player.x>800||player.x<0)player.dx=-player.dx;
		if(player.y>400||player.y<0)player.dy=-player.dy;
		Sleep(200);
	}
	return 0;
}

好的,看完源码了。

C++
1.
void makething(int id,int x,int y){
	RECT rect;
	GetWindowRect(hwnd[id],&rect);
	MoveWindow(hwnd[id],x,y,rect.right-rect.left,rect.bottom-rect.top,TRUE);
}

这个函数是重点,这是让所有窗口移动的关键。至于什么意思吗… 2.GetForegroundWindow(); 这个东西可以获得窗口句柄,获得之后才能移动窗口。

原文