Need help: coding a Python Pyxel program
SolvedEmixWarrior64 -
Good evening,
I am currently coding a program, but I am stuck. The program is in Pyxel, and I am having trouble with the very last part (see def vitesse_ennemis():). What frustrates me greatly is the difficulty management (here the speed of the enemies, I want the enemy to be a bit faster every 10 seconds). If someone could help me please, it’s for a NSI project I'm trying to do.
A little note: I also can't display the score when I lose a life (see the program at the end), if someone could also help with that because honestly I can't manage at all...
Here is the program:
# ****************************************************************************** # ******* Project: game where enemies become faster and faster ******* # ****************************************************************************** import pyxel, random # window size 128x128 pixels - do not modify pyxel.init(128, 128, title="Nuit du code") # initial position of the ship # (origin of positions: top left corner) vaisseau_x = 60 vaisseau_y = 60 # lives vies = 1 # initialization of enemies ennemis_liste = [] # initialization of score score = 0 def vaisseau_deplacement(x, y): """movement with the direction keys""" if pyxel.btn(pyxel.KEY_RIGHT): if (x < 120) : x = x + 1.5 if pyxel.btn(pyxel.KEY_LEFT): if (x > 0) : x = x - 1.5 if pyxel.btn(pyxel.KEY_DOWN): if (y < 120) : y = y + 1.5 if pyxel.btn(pyxel.KEY_UP): if (y > 0) : y = y - 1.5 return x, y def ennemis_creation(ennemis_liste): """random creation of enemies""" # one enemy per second if (pyxel.frame_count % 30 == 0): ennemis_liste.append([random.randint(0, 120), 0]) return ennemis_liste def ennemis_deplacement(ennemis_liste): """movement of enemies upwards and removal if they leave the frame""" for ennemi in ennemis_liste: ennemi[1] += 1 if ennemi[1]>128: ennemis_liste.remove(ennemi) return ennemis_liste def vaisseau_suppression(vies): """disappearance of the ship and an enemy if contact""" for ennemi in ennemis_liste: if ennemi[0] <= vaisseau_x+6 and ennemi[1] <= vaisseau_y+6 and ennemi[0]+6 >= vaisseau_x and ennemi[1]+6 >= vaisseau_y: ennemis_liste.remove(ennemi) vies = 0 return vies def vitesse_ennemis(ennemis_liste): for ennemi in ennemis_liste: ennemi[1] += 1 if (pyxel.frame_count % 30 == 0): ennemi[1] + 1 # ========================================================== # ========= Update of all information ========= # ========================================================== def update(): """update of variables (30 times per second)""" global vaisseau_x, vaisseau_y, ennemis_liste, vies # update the position of the ship vaisseau_x, vaisseau_y = vaisseau_deplacement(vaisseau_x, vaisseau_y) # creation of enemies ennemis_liste = ennemis_creation(ennemis_liste) # update positions of enemies ennemis_liste = ennemis_deplacement(ennemis_liste) # removal of ship and enemy if contact vies = vaisseau_suppression(vies) # ========================================================== # ========================== DRAW ========================== # ========================================================== def draw(): """creation of objects (30 times per second)""" pyxel.cls(0) # if the ship has lives the game continues if vies > 0: # ship (square 8x8) pyxel.rect(vaisseau_x, vaisseau_y, 8, 8, 1) # enemies for ennemi in ennemis_liste: pyxel.rect(ennemi[0], ennemi[1], 6, 6, 4) # else: GAME OVER else: pyxel.text(49,49, 'GAME OVER', 7) pyxel.text(20,60, 'Your score is :', 7) #pyxel.text(30,60, score, 7) print("Your score is:", score) pyxel.run(update, draw)
1 answer
Good evening, to increase the speed of enemies every 10 seconds, you can use a frame counter that checks if the number of frames is divisible by a certain number. You can use this check in your "vitesse_ennemis" function to increase the speed of each enemy in your list, it would look like this:
def vitesse_ennemis(ennemis_liste): # Increase the speed of each enemy every 10 seconds (300 frames) if pyxel.frame_count % 300 == 0: for ennemi in ennemis_liste: ennemi[1] += 1
And to display the score when you lose a life, you can use the "pyxel.text" function in the "draw" function. It allows you to display text on the screen. It would look like this:
def draw(): """display all elements""" global vies, score # clear the screen pyxel.cls(0) # display the ship pyxel.blt(vaisseau_x, vaisseau_y, 0, 0, 0, 7, 7) # display enemies for ennemi in ennemis_liste: pyxel.blt(ennemi[0], ennemi[1], 0, 7, 0, 7, 7) # display the score pyxel.text(0, 0, "Score: " + str(score), 7) # display the number of remaining lives pyxel.text(80, 0, "Lives: " + str(vies), 7) # display the score when you lose a life if vies == 0: pyxel.text(40, 40, "Game Over", 7) pyxel.text(40, 50, "Score: " + str(score), 7)
Keep me updated
Thank you, man, I just saw this, I just tested it and it worked!!!!!!!!