Rendering on a canvas

Yesterday was a great day working with ChatGPT4. I am very happy to have invested those $20 into that plus license - availability and speed are amazing. 

Now that I spent some time creating first, shabby versions of player, monsters, NPCs, trees, walls, stone floor, it's time to do a first simple rendering. 

Nicely, ChatGPT remembers your conversation if you click on it, so I started over where I left a week ago and asked:

can we continue this conversation? Now I have all the images ready to continue; I would like to first start with the code to draw on the Canvas. can you show me an example: assume I have to coordinates xmax and ymax, and I want to draw a grid of 0..xmax-1 by 0..ymax-1 . I have a class called Level with a method getDrawingObjectsAt(x,y) that returns a sorted list of DrawingObject objects that need to be drawn on this coordinate after each other. A DrawingObject has a method getImage() that returns an image URL pointing to a .png file. Can you show me the code to draw this on a canvas?

And it created nice JS code to do just that. I like that I can tell it that there is a class X with method Y that provides the data.

Then I needed many changes to my existing classes - enhance the super class Creature and all subclasses like Warrior to provide the URL of graphics that draw them, and a few more things.

This formula seems to work very well: describe the code change you want. Be sure to NOT rely on history but paste all relevant classes into the chat window. Describe precisely what you want to change.

For example I asked:

Here's a HTML page that you generated and that I then added more stuff to. This is a big mess. It doesn't work, but let's cover this later. My question is: how can I clean up the JS code; ideally I want to have it in a separate JS file, but I need to interact with the canvas. How would you do it?

 (html page snipped)

and it answered:

You can clean up the code by moving the JavaScript code to a separate file, say game.js. You'll still be able to interact with the canvas and other DOM elements in the new file.

First, create a new JavaScript file called game.js and move the script content from the HTML file to it. Make sure to remove the script tags, and keep only the JavaScript code in game.js. Your game.js should look like this:

(extracted JS code)

Next, update your HTML file to include the game.js file as a module. Remove the current script tag and replace it with the following:

<script type="module" src="game.js"></script>
 

Your updated HTML file should look like this:

(html page with removed JS code)

Pretty perfect...

And after fixing a few bugs I had a first level (only floor for now):


Yes, this is all ugly, but hey it has drawn a level!

Happy! :-)

 

Kommentare

Beliebte Posts aus diesem Blog

ChatGPT as a teacher

How it started...

Pathfinding and A*