Oh, that naughty sketch! Please let us know what the issue is below.
Apply Template
Applying this template will reset your sketch and remove all your changes. Are you sure you would like to continue?
Report Sketch
Report Comment
Please confirm that you would like to report the comment below.
We will review your submission and take any actions necessary per our Community Guidelines. In addition to reporting this comment, you can also block the user to prevent any future interactions.
Please report comments only when necessary. Unnecessary or abusive use of this tool may result in your own account being suspended.
Are you sure you want to delete your sketch?
Any files uploaded will be deleted as well.
Delete Comment?
This will also delete all the replies to this comment.
Delete this tab? Any code in it will be deleted as well.
Select a collection to submit your sketch
We Need Your Support
Since 2008, OpenProcessing has provided tools for creative coders to learn, create, and share over a million open source projects in a friendly environment.
Niche websites like ours need your continued support for future development and maintenance, while keeping it an ad-free platform that respects your data and privacy!
Please consider subscribing below to show your support with a "Plus" badge on your profile and get access to many other features!
CC Attribution NonCommercial ShareAlike
Space Game
VITAL
xxxxxxxxxx
let player;
let enemies = [];
let bullets = [];
let bg;
function preload() {
// Load your images
bg = loadImage('Bg.jpg');
playerImg = loadImage('pink_ship_2.png');
enemyImg = loadImage('green_ship_2.png');
fireballImg = loadImage('fireball.png'); // New texture for fireballs
}
function setup() {
createCanvas(800, 600);
player = new Player();
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 4; j++) {
enemies.push(new Enemy(j*160 + 100, i*70 + 70));
}
}
}
function draw() {
background(bg);
player.show();
player.move();
for (let enemy of enemies) {
enemy.show();
enemy.move();
}
for (let bullet of bullets) {
bullet.show();
bullet.move();
if (bullet.hits(player)) {
console.log("Player hit!");
}
for (let enemy of enemies) {
if (bullet.hits(enemy)) {
bullets.splice(bullets.indexOf(bullet), 1);
enemies.splice(enemies.indexOf(enemy), 1);
break;
}
}
}
}
function keyPressed() {
if (key === ' ') {
bullets.push(new Bullet(player.x, height - 50));
}
}
class Player {
constructor() {
this.x = width / 2;
this.y = height - 80; // Adjust the player's starting position
}
show() {
image(playerImg, this.x, this.y, 100, 100); // Increase the size of the player's ship
}
move() {
if (keyIsDown(LEFT_ARROW)) {
this.x -= 5;
}
if (keyIsDown(RIGHT_ARROW)) {
this.x += 5;
}
}
}
class Enemy {
constructor(x, y) {
this.x = x;
this.y = y;
this.direction = 1; // Added direction for left to right movement
this.speed = 1; // Added speed for slower movement
}
show() {
image(enemyImg, this.x, this.y, 100, 100); // Increase the size of enemy ships
}
move() {
this.x += this.direction * this.speed;
if (this.x > width - 100 || this.x < 0) {
this.direction *= -1; // Reverse direction at screen edges
this.y += 10; // Move down when changing direction
}
}
}
class Bullet {
constructor(x, y) {
this.x = x;
this.y = y;
}
show() {
image(fireballImg, this.x, this.y, 20, 20); // Set the size of fireballs and use the new texture
}
move() {
this.y -= 5;
}
hits(target) {
let d = dist(this.x, this.y, target.x, target.y);
return d < 50; // Adjust this value as needed for accurate hit detection
}
}
See More Shortcuts