What if you move the background image position, but keep the player in the same place (in the centre of the screen)? So, the background image is larger than the viewable area, and you pan it about. In other words, your x/y variables control the background coordinates, not the player’s.
You need to assign your background position to be some x - some offsetx and some y - some offsety. Where offset y and offsetx is calculated when you press the arrow keys and therefore gets updated. Its the same principle as drag and drop ill post some code later
def draw():
...
# your x/y variables control the background coordinates
image(background_image, x, y)
with pushMatrix():
# keep the player in the same place (in the centre of the screen)
translate(width/2, height/2)
angle = atan2(mouseX-width/2, mouseY-height/2)
rotate(angle*-1)
image(player_image, -50, -50, 100, 100)
...
However, your movement will be inverted. You’ll need to change y -= speed to y += speed, and so on.
Maybe an additional tilemap-type layer – one that overlays your entire stage with tiles ranging between solid black and fully-transparent. These could include gradient ‘edge’ tiles.
I didn’t understand why the coordinates of image in your code is negative (i.e. (player_image, -50, -50, 100, 100)). Can you explain pls. It works, but I want to understand.
Also, can you tell me the best and easiest way to place enemies around (non-movable) and after 3 gunshots, kill it?
The negative x-y coordinates for player_image offset the point of rotation, so that it pivots around its centre. If you set those coordinates to (0, 0), the player image pivots on its top-left corner:
Left image: image(player_image, -50, -50, 100, 100)
Right image: image(player_image, 0, 0, 100, 100)
It’s probably best to create a new class of enemy objects:
class Enemy(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.lives = 3
def display(self):
circle(self.x, self.y, 50)
Each enemy begins with 3 lives, stored in the self.lives attribute.
You’ll want the enemy objects to pan about with your stage/background, so you adapt your code to use another pushMatrix() –
...
enemy1 = Enemy(200, 300)
enemy2 = Enemy(400, 120)
...
def draw():
...
# your x/y variables control the background coordinates
with pushMatrix():
translate(x, y)
image(background_image, 0, 0)
enemy1.display()
enemy2.display()
...
I’m not sure what type of detection you’ll use for bullet-enemy collisions. For simplicity, I’d recommend AABB or circle-circle collision – depending which shape conforms best to your bullets and enemies. Again, I’d recommend a new class for bullet objects. Once you’ve got all that working, you can look at deducting enemy lives each time one is struck with a bullet.
# this requires no popMatrix()
with pushMatrix():
translate(10, 10)
square(0, 0, 50)
# this is the popMatrix() alternative
pushMatrix()
translate(10, 10)
square(0, 0, 50)
popMatrix()
yeah it works now! Thanks! In my game, I’m trying to implement a clock/timer. It works as of now but only problem is I need to make the frameRate to 1 but it affects the whole program’s frameRate. How do I change frameRate only for the clock function?