Issue with integrating a JavaScript game into a Django template

Recently, I set up a Django server for my personal/local use and wanted to incorporate an interactive game into it (not for deployment, just for myself). After some searching, I came across this cool open-source game: https://github.com/MattSkala/html5-bombergirl.git

Although I was able to run the game from a simple HTML page, integrating it into a Django template proved to be challenging.

Here is the valid HTML code where the game launches successfully in a web browser:

<!doctype html>
<html>
<head>
    <script type="text/javascript" src="bower_components/EaselJS/lib/easeljs-0.8.1.min.js"></script>
    <script type="text/javascript" src="bower_components/PreloadJS/lib/preloadjs-0.6.1.min.js"></script>
    <!-- more script imports -->
  
    <link rel="stylesheet" type="text/css" href="bower_components/bootstrap/docs/assets/css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="css/style.css" />
</head;

<canvas id="canvas" width="545" height="416"></canvas>
<script>
  gGameEngine = new GameEngine();
  gGameEngine.load();
</script>
</html>

In my Django setup, I made sure to update and load all the required static files (JavaScript, CSS, bower_components).

This is my views.py in Django:

def game(request):
  return render(request, 'game.html')

And here's my game.html template:

{% extends "base.html" %}
{% block content %}

<canvas id="canvas" width="545" height="416"></canvas>
<script>
  gGameEngine = new GameEngine();
  gGameEngine.load();
</script>
{% endblock %}

Unfortunately, my page remains blank with no game loading or errors on the webpage or terminal. It seems as though nothing is happening at all.

I even attempted to add a function within the GameEngine.js file:

function my_run()
{
  //document.getElementById("canvas").innerHTML = "Hi there, do I work ?";
  gGameEngine = new GameEngine();
  document.getElementById("canvas").innerHTML = gGameEngine.load()
}

Then adjusted my template to include:

<script src = {% static "game/js/GameEngine.js" %}> </script>
//<p id="canvas">Text for test.</p>
<canvas id="canvas" width="545" height="416"></canvas>
<button type = "button" onClick="my_run()">Click me!</button> 

But still, no success!

Interestingly, displaying "Hi there, do I work?" in the "Text for test" paragraph works fine, indicating that the static files are being loaded correctly. However, the game itself still doesn't run. Any ideas on what could be causing this issue?

Thank you in advance for your assistance.

Answer №1

The issue has been successfully resolved. I simply needed to update the file paths of the images and sounds to ensure compatibility with django.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Every time I restart VSCode, I have to re-run the .zsh_profile command in order for the NVM packages to work properly

Many others have encountered a similar issue, but I'm struggling to resolve it. Every time I open VSCode, I find myself needing to run these commands in the terminal for npx, npm, and nvm to work: export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] ...

Introducing LightZAP v2.54 (silent update2), the revolutionary Lightbox custom plugin that seamlessly launches when the page loads using the designated tag name (#img1)

As I have the LightZAP plugin installed on my website as an alternative to Lightbox, I am looking to trigger it (to display an image gallery with a specific image) when the page loads using the following link: index.html#img1, if possible. I have come acr ...

Adjust alterations in a Vue Component to apply to separate routes

I have a Filter tab component that I use in various routes. When I click on a tab, it becomes active. After clicking on one tab, I want it to remain active in other routes as well. How can I achieve this? Any suggestions or articles would be greatly apprec ...

Which programming language or technology utilizes the syntax <%VARIABLE%> and in what context is it employed?

First things first, I want to clarify that I'm not a developer, so this might be a simple question. Despite my research indicating that ASP typically uses this syntax, it's important to note that it is not the case here. I am currently delving i ...

Acquire by Identifier - Tonic()

Currently, I am in the process of setting up a code example on tonicdev.com for my open-source React component available on Npm. Below is the code snippet that I am attempting to execute (editable live on tonicdev.com here): var React = require('rea ...

Ways to delete a header from the req object in Express

Can anyone help me understand how to remove a header from the req object in Express? I've heard that using res.disable("Header Name") can do this for the res object, but it doesn't seem to work for req.headers. ...

What is the best way to pass a chosen value to php through ajax?

Trying to pass the selected item value from a select tag using AJAX. Here is the select tag (not in a form): <select class="select" id="select"> </select> Code to populate the select from the database using PHP: $(document).ready(function(){ ...

React Leaflet causing a frequent map refresh due to context value updates

When I use map.on('moveend') to update the list of markers displayed in another component, I encounter a refreshing issue. The context in my project contains two arrays - one filtered array and one array with the markers. When I try to update the ...

What is the best way to loop through all the configurations in Django?

Is there a way to loop through all Django settings? I want to access settings that start with MYAPP_. I attempted the following method: from django.conf import settings for setting in settings: print(setting) However, this resulted in an exception: ...

How to Show Extensive Content in an Android WebView with a Fixed Height

Is anyone familiar with how to make a large HTML page fit completely within a web-view on an Android device without any vertical scroll bars? I want the entire webpage to be displayed within the varying height of the web-view when the user clicks "fill hei ...

Exploring FabricJs: Reassessing the coordinates of an object post-rotation

const canvas = new fabric.Canvas("c", { selection: false }); const refRect = new fabric.Rect({ left: 250, top: 150, width: 200, height: 100, fill: 'transparent', stroke: 'blue', originX: 'center', originY: & ...

Guide to cycling through an array in Angular template in order to display assorted colored tiles with a consistent pattern

View images in tile format Here are the specific colors assigned to each tile when looping through 0 to 11, where there are 12 records in the array for each tile: - Red color for tiles at index: 0, 4, 8, and 12 - Green color for tiles at index: 1, 5, an ...

Can the image be adjusted based on different time zones around the world?

How can I create an HTML banner that changes images based on the time of day? I want one image to display between 7pm and 6am, and another image during the rest of the day. I came across a helpful website that achieves this by changing the image according ...

How to use a self-hosted font in a custom Material-UI theme for ReactJS?

Currently, I am in the process of setting up my initial project utilizing Material-UI for ReactJS. My intention is to personalize the default theme by incorporating a custom font (from the server, not sourced from Google Fonts or anything similar). Despite ...

How can I issue a query in Django?

In my database, there are two tables named Location and Rate. The Rate table includes the following items: The field von_location serves as a Foreignkey to the Location table. I am looking to retrieve a location based on a specific rate. For instance, w ...

Transform text to lowercase and eliminate whitespace using JavaScript

I am a newcomer to the world of JavaScript and currently focused on building Discord bots. I have successfully coded a bot that responds to messages, but I'm facing issues when the input is in capital letters or contains spaces. The bot fails to res ...

Utilize the power of Javascript/AJAX or jQuery to submit multiple forms simultaneously

I am currently working on a project which involves having 3 forms on a single page. The reason behind this is that one of the forms is displayed in a modal dialog. My goal is to allow users to submit all 3 forms with a single button click, and I also wan ...

Using javascript to add SVGs with duplicate IDs

When SVGs are inserted into a document using javascript, they may appear as gibberish due to conflicting IDs within them. For example, consider these two distinct svg files: Upon insertion into the document using javascript*, one of them may display incor ...

Using AngularJS to Extract JSON Data from a Table in an HTML Document

In the context of angularjs: I have a table with 2 fixed columns (ID & Comment) and additional columns that are added dynamically by the user clicking a button. The user can input data into the rows of this table, and I need to extract/ read this data. H ...

For an optimal printing experience with Jqprint, it is recommended to utilize color output

Currently, I am exploring the jqprint plugin and am quite impressed with its functionality, except for one issue. I am trying to add color to the object that I want to print, but my attempts to colorize the object before sending it to Jqprint have been uns ...