d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help Me Understand Why My Game Sucks. =) > Javascript
Add Reply New Topic New Poll
Member
Posts: 29,418
Joined: Mar 27 2008
Gold: 504.69
Sep 2 2014 08:02pm
Bored one day and wanted to see if I could make an html5 game.
Super noob but would appreciate if anyone could tell me why my code is flawed (which it undoubtedly is) so I could learn from it.

My goal was simply to make a character walk across the screen.
I downloaded a Megaman 8-bit sprite-sheet and used that. http://www.sprites-inc.co.uk/files/Classic/Megaman/MM8/megaman8bit.png

HTML:
Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Game</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>

<body>
<canvas id="game" width='800' height='600' style="border:2px solid #000000;"></canvas>
<script type="text/javascript" src="javascript.js"></script>
</body>
</html>


CSS:
Code
@charset "utf-8";
/* CSS Document */

canvas {
padding-left: 0;
padding-right: 0;
margin-left: auto;
margin-right: auto;
display: block;
}


Javascript:
Code
// JavaScript Document
var canvas = document.getElementById('game');
var context = canvas.getContext('2d');

// loads player
//load spritesheet into a variable
var player = new Image();
player.src = 'images/megaman8bit.png';
//define intital player position
var playerx = 1
var playery = 576
//load intial image of player
player.onload = function() {
context.drawImage(player, 103, 9, 20, 24, playerx, playery, 20, 24);
};
//define variables for movement
var playerright = 0

//check keypress
document.onkeydown = checkKey;
function checkKey(e) {
e = e || window.event;

if (e.keyCode == '39') {
// right arrow
canvas.width = canvas.width;
right();
if(playerright == 0){
context.drawImage(player, 188, 9, 23, 24, playerx, playery, 23, 24);
right1();
}
else if(playerright == 1){
context.drawImage(player, 218, 9, 16, 24, playerx, playery, 16, 24);
right1();
}
else if(playerright == 2){
context.drawImage(player, 238, 9, 16, 24, playerx, playery, 16, 24);
rightreset();
}
}
}

//movement
//right
//define movement right in pixels
function right() {
playerx = playerx + 10;
}
function right1(){
playerright = playerright + 1;
}
function rightreset(){
playerright = 0;
}
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Sep 3 2014 01:18am
Ask this in the web programming section
Member
Posts: 24,488
Joined: Jul 11 2011
Gold: 1,272.50
Sep 3 2014 02:19am
I tested out your code.

Looks fine to me, I mean, your guy does walk across the screen. You just need to add functions for jumping, attacking, moving left, up, down, mazes, doors, collision detection for outside posterior walls, a ton of other crap.

What I recommend is just get a html 5 game engine and add and craft functions ontop of that. Don't try to reinvent the wheel. (Yes I know it's fun to code and learn new things along the way) but you can do that with the engines as well. Plus it will save you a ton of time.
Not everyone is a njaguar and can just craft up custom coded games.

This post was edited by HighschoolTurd on Sep 3 2014 02:23am
Member
Posts: 29,418
Joined: Mar 27 2008
Gold: 504.69
Sep 3 2014 06:45am
Quote (HighschoolTurd @ Sep 3 2014 04:19am)
I tested out your code.

Looks fine to me, I mean, your guy does walk across the screen. You just need to add functions for jumping, attacking, moving left, up, down, mazes, doors, collision detection for outside posterior walls, a ton of other crap.

What I recommend is just get a html 5 game engine and add and craft functions ontop of that.  Don't try to reinvent the wheel.  (Yes I know it's fun to code and learn new things along the way) but you can do that with the engines as well.  Plus it will save you a ton of time.
Not everyone is a njaguar and can just craft up custom coded games.


Thanks bro. That's where I started but it got a little confusing so I did this to get my feet wet so to speak. Any engine suggestions for a Zelda-clone?
Member
Posts: 24,488
Joined: Jul 11 2011
Gold: 1,272.50
Sep 3 2014 09:57pm
Quote (ROM @ 3 Sep 2014 05:45)
Thanks bro. That's where I started but it got a little confusing so I did this to get my feet wet so to speak. Any engine suggestions for a Zelda-clone?


Just check here

http://html5gameengine.com/

Look through each one. I'm not that versed on which one is the best for a zelda like clone. Are u talking about 3d rendering zelda or the old snes one's? https://playcanvas.com/ Here is if you want a Zelda n64 type clone I imagine.

Good luck!

This post was edited by HighschoolTurd on Sep 3 2014 09:58pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll