Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Space Invaders
#6
(Jan-02-2020, 08:03 PM)MelihsahCelik Wrote:
(Jan-01-2020, 11:18 PM)michael1789 Wrote: no, your line 26 is you making an empty list.
m=[]
and then on line 32...
choix1=random.choice(m)
You are taking a random choice from that empty list.

That's why you get the error
Error:
IndexError: Cannot choose from an empty sequence
The empty sequence is "m=[]".

Thanks for your reply and your help I tried to change and add some codes to see if it works but it still don't work. I'm new to Python so I don't really know what to do now :/ (I'm only doing 2 hours per week in school and sometimes at home).

It works I changed my code and it is working now :) Thanks for your help. If anyone needs my new code is this :
import pygame,sys
from pygame.locals import *
import random

pygame.init()
pygame.font.init()
mainclock= pygame.time.Clock()
window=pygame.display.set_mode((450,560,),0,32)     	# Taille fenêtre du jeu
pygame.display.set_caption('SpaceInvader')          	# titre de la fenêtre
xs=20							# coordonnées de départ du vaisseau
ys=500
i=0
missile=[]
Gauche=False
Droite=False
Feu = False
Texty = pygame.font.Font('SUPERPOI_R.TTF', 10)
Obj_texte = Texty.render('les envahisseurs', 0, (0,0,255))
clock = pygame.time.Clock()
Movespeed=4
toile=pygame.image.load('toile.png')
background=pygame.image.load('background.png')
ship1=pygame.image.load('ship1.png')
ship2=pygame.image.load('ship2.png')
meteor=pygame.image.load('meteor.png')

cible=[]
invader1=pygame.image.load("invader1.png")
invader2=pygame.image.load("invader2.png")
i=0
for n in range(200,400,100):
    for i in  range(20,300,50):
        cible.append(pygame.Rect(i,n,32,24))    # Liste des rectangles des images cibles

tir_cible=[]
temptir =  i + random.randint(15,60)
def ajouter_temptir():
       	choix=random.choice(cible)
        tir_cible.append(pygame.Rect(choix.left+12,choix.bottom,6,16))

tir_meteor=[]
temptir1 =  i + random.randint(15,60)
def ajouter_temptir1():
       	choix1=random.choice(cible)
        tir_meteor.append(pygame.Rect(choix1.left+12,choix1.bottom,6,16))

while True:
    window.blit(background,(0,0))	    # Image arrière plan
    window.blit(Obj_texte,(50,20))			# Placer le texte

    for alien in cible:                         #deplacement des aliens
        alien.left+=Movespeed
    for alien in cible:
        if alien.right>450 or alien.left<0:      #deplacement sur le cote
            Movespeed*=-1
            break

    if temptir <= i:
	       temptir =  i + random.randint(15,60) #Tir des aliens
	       ajouter_temptir()

    if temptir1 <= i:
	       temptir1 =  i + random.randint(15,60) #Tir des aliens
	       ajouter_temptir1()

    for event in pygame.event.get():			# Traiter les évènements du clavier
        if event.type== KEYDOWN:
            if event.key==K_ESCAPE:
               pygame.quit()
               sys.exit()
            if event.key==K_RIGHT:
                Droite = True
            if event.key==K_LEFT:
                Gauche = True

        if event.type== KEYUP:
            if event.key==K_RIGHT:
                Droite = False
            if event.key==K_LEFT:
                Gauche = False
            if event.key==K_SPACE:
                missile.append(pygame.Rect(xs+8,ys,6,16))

    i=i+1
    if Gauche and xs>10: xs =xs-10
    if Droite and xs <400 : xs +=10

    for tir in missile:					# pour chaque missile existant
        tir.top=tir.top-20				# soustraire 10 à la coordonnée du point haut
        window.blit(toile,tir)		# toile d'arraignée
        if tir.top==100:				# si le missile arrive en haut de l'écran
            missile.remove(tir)				# supprimer le missile de la liste
        for c in cible:
            if tir.colliderect(c):
                missile.remove(tir)
                cible.remove(c)

    for tir in tir_cible:
        tir.top=tir.bottom+20
        window.blit(toile,tir)
        if tir.bottom==-20:
            tir_cible.remove(tir)

    for m in tir_meteor:
        m.top=m.bottom+20
        window.blit(meteor,m)
        if m.bottom==-20:
            tir_meteor.remove(m)


    if int(i/20)%2 ==1:
     window.blit(ship1,(xs,ys))
    else:
     window.blit(ship2,(xs,ys))

    for c in cible:
        if int(i/20)%2 ==1:
            window.blit(invader1,c)
        else:
            window.blit(invader2,c)


    pygame.display.update()
    clock.tick(30)
Instead of using the empty "m" sequence I created another one "tir_meteor". Smile
Reply


Messages In This Thread
Space Invaders - by MelihsahCelik - Dec-31-2019, 03:54 PM
RE: Space Invaders - by ndc85430 - Dec-31-2019, 05:09 PM
RE: Space Invaders - by MelihsahCelik - Jan-01-2020, 09:10 PM
RE: Space Invaders - by michael1789 - Jan-01-2020, 11:18 PM
RE: Space Invaders - by MelihsahCelik - Jan-02-2020, 08:03 PM
RE: Space Invaders - by MelihsahCelik - Jan-02-2020, 10:53 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020