RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-366830

h4cktivist's questions

Martin Hope
h4cktivist
Asked: 2020-04-29 11:37:37 +0000 UTC

如何在 Linux 上的 C++ 中使用 kbhit() 和 getch()

  • 0

我正在为本教程用 C++ 编写一条小“蛇”。面临这样的问题,我需要使用kbhit()和getch()阅读用户输入。这些功能在库conio.h中,而 Linux 中没有。另外,我试过这个,但它没有帮助,程序刚刚停止,当你输入时什么也没发生。

我想知道如何在 Linux 上实现它,或者这个库有什么替代品吗?

编码:

#include <iostream>
#include <conio.h>
using namespace std;

bool GameOver;
const int height = 20;
const int width = 20;
int x, y, fruit_x, fruit_y, score;
enum eDirection { STOP, RIGHT, LEFT, UP, DOWN };
eDirection dir;

void setup() {
    GameOver = false;
    dir = STOP;
    x = width / 2 - 1;
    y = height / 2 - 1;
    fruit_x = rand() % width;
    fruit_y = rand() % height;
    score = 0;
}

void draw() {
    system("clear");

    for (int i = 0; i < width; i++)
    {
        cout << "#";
    }
    cout << endl;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if (j == 0 || j == width - 1)
        {
            cout << "#";
        }
        if (i == y && j == x)
        {
            cout << "0";
        }
        else if (i == fruit_y && j == fruit_x)
        {
            cout << "F";
        }
        else
        {
            cout << " ";
        }
    }
    cout << endl;
    }

    for (int i = 0; i < width; i++)
    {
        cout << "#";
    }
    cout << endl;
}

void input() {
    if (_kbhit)
    {
        switch(getch())
    {
        case 'a':
            dir = LEFT;
            break;
        case 'd':
            dir = RIGHT;
            break;
        case 'w':
            dir = UP;
            break;
        case 's':
            dir = DOWN;
            break;
        case 'x':
            GameOver = true;
            break;
        }
    }
}

void logic() {
    switch(dir)
    {
    case LEFT:
        x--;
        break;
    case RIGHT:
        x++;
        break;
    case UP:
        y--;
        break;
    case DOWN:
        y++;
        break;
    }
}


int main() {
    setup();
    while(!GameOver)
    {
        draw();
        input();
        logic();
    }

}
c++
  • 1 个回答
  • 10 Views
Martin Hope
h4cktivist
Asked: 2020-04-18 12:56:42 +0000 UTC

如何在 Tkinter,Python 中将整数值输入到变量中?

  • 0

我正在使用 Tkinter GUI 模块编写一个小型 Python 计算器。我遇到了一个问题:我无法将整数值准确地输入到变量中。尝试添加两个输入的数字时,会出现错误:TypeError: unsupported operand type(s) for +: 'StringVar' and 'StringVar'. 试图使用相同的IntVar- 相同的错误:TypeError: unsupported operand type(s) for +: 'IntVar' and 'IntVar'。我想学习如何将整数值输入变量并与它们进行算术运算。

编码:

from tkinter import *

root = Tk()
root.title('Calculator Pro')
root.geometry("300x300")

def sum():
    print (number1 + number2)

number1 = StringVar()
number2 = StringVar()

number1_entry = Entry(textvariable=number1)
number1_entry.pack()
number2_entry = Entry(textvariable=number2)
number2_entry.pack()
bt = Button(text="Summa", command=sum)
bt.pack()

root.mainloop()

截图(所以,以防万一):

在此处输入图像描述

python
  • 1 个回答
  • 10 Views
Martin Hope
h4cktivist
Asked: 2020-04-12 18:10:41 +0000 UTC

如何在 Python 中与程序并行播放声音?

  • 1

我使用 PyGame 库在 Python 中编写了一个小游戏。一切正常,然而,与表达相反:“它有效 - 不要触摸它”,我决定为游戏添加背景音乐。这就是问题开始的地方,我尝试了不同的库来播放声音,例如:playsound, pyglet,但是到处都出现了同样的问题。当游戏本身启动时,音乐开始播放,但我无法完成所有游戏动作(行走角色等)。我尝试为播放创建一个单独的函数并在代码的不同部分(最后、开头、中间)激活它,但问题仍然存在。

我想知道可以使用哪个库或如何使用我错过的库,只是为了我可以执行游戏中的所有动作,但同时播放音乐。

python
  • 2 个回答
  • 10 Views
Martin Hope
h4cktivist
Asked: 2020-04-02 12:42:18 +0000 UTC

不在 C++ 代码中添加一些 {} 在美学上有多令人愉悦?

  • 5

我开始学习C++。在学习的过程中,我注意到了一件事情,即不放花括号({})来限制的能力,例如循环体,分支等。同时,在编译过程中没有问题,没有花括号的代码和有花括号的代码一样正确。

我想知道从代码美学的角度来看这是多么正确,使用哪个选项以及是否至少有一些区别。

例如,这是带有花括号的代码:

#include <iostream>
using namespace std;

int main() {

    while (true)
    {
        cout << "Hello, world" << endl;
    }

}

没有花括号:

#include <iostream>
using namespace std;

int main() {

    while (true)
        cout << "Hello, world" << endl;

}
c++
  • 2 个回答
  • 10 Views
Martin Hope
h4cktivist
Asked: 2020-02-06 16:51:41 +0000 UTC

在 Sublime Text 3 中编译时的输入问题

  • 0

在 Sublime Text 3 编辑器中编译代码时出现问题。即:(我展示了 C++ 中的计算器示例)当你输入一个动作时(通过按 Enter),没有任何反应,只有光标移动到下一行。

编译所有需要键盘输入的程序(我在 Python 和 C++ 中尝试过)时都会出现这个问题。

ps 如果有人需要:那么我用于 C++ 的编译器是 g++。

我请您帮助解决问题,而不是要求使用其他 IDE。谢谢大家 :)

компиляция
  • 1 个回答
  • 10 Views
Martin Hope
h4cktivist
Asked: 2020-02-02 00:11:50 +0000 UTC

连接brain.js 库的问题

  • 0

用JavaScript写了一个神经网络(应该可以检测图片),但是在连接brain.js库本身的时候出现了问题。在浏览器控制台中显示:“在 'file:///brain.js' 加载失败。” 该链接有效,我通过将其插入浏览器的地址栏中单独检查它。请帮助解决这个问题,提前谢谢大家:) 代码附在下面:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>НС</title>

	<script src="//unpkg.com/brain.js"></script>

	<style>
		body {
			background-color: #333;
		}

		#canv {
			position: absolute;
			top: 0;right: 0;bottom: 0;left: 0;
			margin: auto;
			background-color: white;
		}
	</style>
</head>
<body>
	
	<canvas id="canv" style="display: block;">Ваш браузер устарел, обновитесь.</canvas>

	<script>
		function DCanvas(el)
		{
			const ctx = el.getContext('2d');
			const pixel = 20;

			let is_mouse_down = false;

			canv.width = 500;
			canv.height = 500;

			this.drawLine = function(x1, y1, x2, y2, color = 'gray') {
				ctx.beginPath();
				ctx.strokeStyle = color;
				ctx.lineJoin = 'miter';
				ctx.lineWidth = 1;
				ctx.moveTo(x1, y1);
				ctx.lineTo(x2, y2);
				ctx.stroke();
			}

			this.drawCell = function(x, y, w, h) {
				ctx.fillStyle = 'blue';
				ctx.strokeStyle = 'blue';
				ctx.lineJoin = 'miter';
				ctx.lineWidth = 1;
				ctx.rect(x, y, w, h);
				ctx.fill();
			}

			this.clear = function() {
				ctx.clearRect(0, 0, canv.width, canv.height);
			}

			this.drawGrid = function() {
				const w = canv.width;
				const h = canv.height;
				const p = w / pixel;

				const xStep = w / p;
				const yStep = h / p;

				for( let x = 0; x < w; x += xStep )
				{
					this.drawLine(x, 0, x, h);
				}

				for( let y = 0; y < h; y += yStep )
				{
					this.drawLine(0, y, w, y);
				}
			}

			this.calculate = function(draw = false) {
				const w = canv.width;
				const h = canv.height;
				const p = w / pixel;

				const xStep = w / p;
				const yStep = h / p;

				const vector = [];
				let __draw = [];

				for( let x = 0; x < w; x += xStep )
				{
					for( let y = 0; y < h; y += yStep )
					{
						const data = ctx.getImageData(x, y, xStep, yStep);

						let nonEmptyPixelsCount = 0;
						for( i = 0; i < data.data.length; i += 10 )
						{
							const isEmpty = data.data[i] === 0;

							if( !isEmpty )
							{
								nonEmptyPixelsCount += 1;
							}
						}

						if( nonEmptyPixelsCount > 1 && draw )
						{
							__draw.push([x, y, xStep, yStep]);
						}

						vector.push(nonEmptyPixelsCount > 1 ? 1 : 0);
					}
				}

				if( draw )
				{
					this.clear();
					this.drawGrid();

					for( _d in __draw )
					{
						this.drawCell( __draw[_d][0], __draw[_d][1], __draw[_d][2], __draw[_d][3] );
					}
				}

				return vector;
			}

			el.addEventListener('mousedown', function(e) {
				is_mouse_down = true;
				ctx.beginPath();
			})

			el.addEventListener('mouseup', function(e) {
				is_mouse_down = false;
			})

			el.addEventListener('mousemove', function(e) {
				if( is_mouse_down )
				{
					ctx.fillStyle = 'red';
					ctx.strokeStyle = 'red';
					ctx.lineWidth = pixel;

					ctx.lineTo(e.offsetX, e.offsetY);
					ctx.stroke();

					ctx.beginPath();
					ctx.arc(e.offsetX, e.offsetY, pixel / 2, 0, Math.PI * 2);
					ctx.fill();

					ctx.beginPath();
					ctx.moveTo(e.offsetX, e.offsetY);
				}
			})
		}

		let vector = [];
		let net = null;
		let train_data = [];

		const d = new DCanvas(document.getElementById('canv'));

		document.addEventListener('keypress', function(e) {
			if( e.key.toLowerCase() == 'c' )
			{
				d.clear();
			}

			if( e.key.toLowerCase() == 'v' )
			{
				vector = d.calculate(true);
				
				//train
				if( confirm('Positive?') )
				{
					train_data.push({
						input: vector,
						output: {positive: 1}
					});
				} else
				{
					train_data.push({
						input: vector,
						output: {negative: 1}
					});
				}
			}

			if( e.key.toLowerCase() == 'b' )
			{
				net = new brain.NeuralNetwork();
				net.train(train_data, {log: true});

				const result = brain.likely(d.calculate(), net);
				alert(result);
			}
		});
	</script>

</body>
</html>

javascript
  • 1 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5