>>281496
>Learning Python first will teach you bad habits
>C is good, honestly. It's better to learn with a more technical low-level or mid-level language and then go to higher level languages
Even though I'm a C purist now, I wouldn't recommend it as a first language. There's already way too many things to learn for making games, using C will multiply that amount by 5. I started with GUI game maker programs where I never programmed a single line of code, then moved to Javascript browser games, and then moved to C after several years. That's probably far from the optimal path, but there's no way I would have had enough motivation if I started with C.
My personal recommendation is that unless you WANT to learn C and/or love it at first sight, start with something that makes it maximally easy to put graphics on the screen (such as Javascript with the canvas API on the browser, Love2D, or some other game engine), and just screw around making little games.
When you're comfortable with making things happen using code, then learn C, and specifically C instead of C++. Learn it even if you don't want to use it because it'll force you to see the world through a simpler and more data-oriented lens, and that's more valuable than actually using C.
Here's a videogame, save this as game.html and open it in a web browser that lets you run html files directly:
<html><head><script>
let key = {};
let bullets = [];
let canvas;
let ctx;
let player = { x:400, y:500, w:30, h:20, speed:4, velocity:0, shotSpeed:6, lastShotTime:0, reload:400, hp:2, dead:false, color:"#0ff" };
let enemy = { x:200, y:100, w:30, h:20, speed:3, velocity:0, shotSpeed:6, lastShotTime:0, reload:350, hp:6, dead:false, color:"#ff0" };
let gamestatetext = "Game on!";
window.onload = function() {
canvas = document.body.appendChild(document.createElement("canvas"));
canvas.width = 600;
canvas.height = 600;
ctx = canvas.getContext("2d");
setInterval(update, 16);
}
document.addEventListener('keydown', function (event) {
switch (event.keyCode) {
case 32: key.space = true; break;
case 65: key.A = true; break;
case 68: key.D = true; break;
case 83: key.S = true; break;
case 87: key.W = true; break;
default: break;
}
});
document.addEventListener('keyup', function (event) {
switch (event.keyCode) {
case 32: key.space = false; break;
case 65: key.A = false; break;
case 68: key.D = false; break;
case 83: key.S = false; break;
case 87: key.W = false; break;
default: break;
}
});
function collision (a, b) {
if (
b.dead ||
(a.origin === b) ||
((a.y + a.h) <= b.y) ||
(a.y >= (b.y + b.h)) ||
((a.x + a.w) <= b.x) ||
(a.x >= (b.x + b.w)))
{
return false;
}
}
function drawMe (me) {
ctx.fillStyle = me.color;
ctx.fillRect(me.x, me.y, me.w, me.h);
ctx.fillText(me.hp, me.x+me.w+5, me.y);
}
function update () {
ctx.font = "14px Arial";
ctx.fillStyle = "#111";
ctx.fillRect(0, 0, 600, 600);
if (!player.dead) {
if (key.A) {player.velocity = -player.speed;}
else if (key.D) {player.velocity = player.speed;}
else {player.velocity = 0;}
player.x = Math.min(570, Math.max(10, player.x+player.velocity));
if (key.space && player.lastShotTime < Date.now()-player.reload) {
bullets.push({x:player.x, y:player.y, w:10, h:10, velocity:-player.shotSpeed, origin:player});
player.lastShotTime = Date.now();
}
drawMe(player);
}
if (!enemy.dead) {
enemy.velocity += Math.min(2, Math.max(-2, Math.random()-0.5));
enemy.x += Math.min(1, Math.max(-1, enemy.velocity))*enemy.speed;
if (enemy.x < 10) {enemy.velocity = 2;}
if (enemy.x > 570) {enemy.velocity = -2;}
if (enemy.lastShotTime < Date.now()-enemy.reload) {
bullets.push({x:enemy.x, y:enemy.y, w:10, h:10, velocity:enemy.shotSpeed, origin:enemy});
enemy.lastShotTime = Date.now();
}
drawMe(enemy);
}
for (let b in bullets) {
let bullet = bullets[b];
bullet.y += bullet.velocity;
ctx.fillStyle = bullet.origin.color;
ctx.fillRect(bullet.x, bullet.y, bullet.w, bullet.h);
if (collision(bullet, player) !== false) {
bullets.splice(b, 1);
b --;
player.hp --;
if (player.hp < 1){
gamestatetext = "You suck, faggot.";
player.dead = true;
}
}
if (collision(bullet, enemy) !== false) {
bullets.splice(b, 1);
b --;
enemy.hp --;
if (enemy.hp < 1){
gamestatetext = "You are the win!";
enemy.dead = true;
}
}
if (collision(bullet, {x:0,y:0,w:canvas.width,h:canvas.height}) === false) {
bullets.splice(b, 1);
b --;
}
}
ctx.fillStyle = "#fff";
ctx.fillText("Bullets: " + bullets.length, 20, 60);
ctx.font = "20px Arial";
ctx.fillText(gamestatetext, 20, 30);
}
</script></head></html>