Hello I attempt to create a game

hello I am starting to create a simple js game using canvas it is my first contribution towards projects like this and something is certainly not working i have created everything as i thought it should be but it is not drawing the image i am trying to draw for some reason. here is link to my github GitHub - lolek55507/shpisGame

If you add the width and height to the canvas you can see the image.

let canvasWidth = 1000;
let canvasHeight = 600;
canvas.width = canvasWidth;
canvas.height = canvasHeight;

I don’t really use, or know much about using Canvas, to be honest.

When you’re ready to migrate, I recommend picking up a library like PixiJS or Phaser to make games given there are a multitude of resources for them.

Hello! One way to fix your code would be not setting the width and height of the using css but manually embedding the values in html as so: as if you do it with css it stretches the pixels

XD thank you I added a background images and they looked like crap and you gave me a fix <3

thank you all for help but i have another issue with the code i have created it the code looks to work just fine for few moments but after a moment of playing it return an error that says that bullets[i].x is not defined i don t get why and have no idea how to fix it so I would appreciate any help and advices GitHub - lolek55507/shipsGame13

Can you open a formal issue for this? It’ll be great practice. And provide some context about when this occurs.

I don’t see that error. How do you reproduce it?

In general, you want to guard against accessing values that can be invalid. Such as using optional chaining and conditionals/guards.

If you tried writing it in TypeScript it would likely yell at you in different places and force you to check the values (as well as demand types of course).

It would also catch errors like this one.

if (enemies.length !== [])

This comparison appears to be unintentional because the types ‘number’ and ‘never’ have no overlap.ts(2367)
This condition will always return ‘true’ since JavaScript compares objects by reference, not value.ts(2839)

const enemies = []
enemies.length !== []

I don t think I ever used typescript so I can t agree or agree with you. I have changed the code many times and the error was still there I’ve tried typing this if (enemies.length != 0) and changing it in many different variations and I have no idea why the error was there until I removed the splice method that was removing the bullets from screen then that error disappears but it is bad solution looking at long distance because it creates a giant array so the issue is probably caused by removing some elements from an array and changing that array’s length, for now I left it like that if someone has any better ideas I would loudly appreciate them. and also I don t know how would I implement optional chaining to this code I have an array that when becomes empty or when something is removed from it causes an error in the part of code that looks for its elements positionX and positionY properties. GitHub - lolek55507/shpisGame19. The another issues is that I have downloeaded a ncs song from youtube and tried to play it reapedly durnig the game is open but it doesn t seem to work either when i m looping that in html same in js it works on my pc if I open it with local server but when I share it on github it stops working.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.