Aircraft wars
#coding =utf-8import pygamefrom pygame.locals import *import timeimport randomclass Base (object): Def __init__ (Self,x,y, Screen,image_name): Self.x=x self.y=y self.screen=screen self.image=pygame.image.load (image_na Me). CONVERT () class Basebullet (Base): def __init__ (self,x,y,screen,image_name): base.__init__ (Self,x,y,screen,ima GE_NAME) def display (self): Self.screen.blit (Self.image, (SELF.X,SELF.Y)) class Bullet (Basebullet): Def __init_ _ (Self,x,y,screen): basebullet.__init__ (self,x+40, y-20, screen, "C:/users/lenovo/desktop/feiji/bullet-3.gif") def move: self.y-=10 def judge (self): if Self.y<0:return True else: Return False #谁发射 who created the class Enemybullet (Basebullet): def __init__ (self,x,y,screen): Basebulle T.__init__ (self,x+25, y+40, screen, "C:/users/lenovo/desktop/feiji/bullet1.png") def Move (self): self.y+=5 Def judge (self): If Self.y>600:return True Else:return False class Baseplane (Base): Def __init__ (self,x,y,screen,image_name): base.__init__ (self,x,y,screen,image_name) self.bullet_list=[] #存储子弹 def di Splay (self): #更新飞机的位置 self.screen.blit (Self.image, (SELF.X,SELF.Y)) #存放需要删除的对象信息 Needdelitemli St=[] for I in Self.bullet_list:if I.judge (): Needdelitemlist.append (i) for i in NeedDelItemList:self.bullet_list.remove (i) #更新及这架飞机发射出的所有子弹的位置 #子弹移动了 determine the position of each bullet and bullet for B Ullet in Self.bullet_list:bullet.display () Bullet.move () class Heroplane (Baseplane): Def __in It__ (self,screen): #默认有照片 default location baseplane.__init__ (Self,200,500,screen, "C:/users/lenovo/desktop/feiji/hero.gif") Self.hit=false #表示是否要爆炸 self.bomb_list=[] #用来存储爆炸时需要的图片 self.__create_images () self.image_num = 0 # The Times used to record while trueNumber, when the number of times reached a certain value to show an exploded figure, and then emptied, when the number of times to reach, and then show the next explosion effect of the picture Self.image_index = 0# used to record the current explosion effect of the image of the number def __create_image S (self): Self.bomb_list.append (Pygame.image.load ("C:/users/lenovo/desktop/feiji/hero_blowup_n1.png")) self.b Omb_list.append (Pygame.image.load ("C:/users/lenovo/desktop/feiji/hero_blowup_n2.png")) Self.bomb_list.append ( Pygame.image.load ("C:/users/lenovo/desktop/feiji/hero_blowup_n3.png")) Self.bomb_list.append (Pygame.image.load ( "C:/users/lenovo/desktop/feiji/hero_blowup_n4.png") def display (self): if Self.hit==true:self.scree N.blit (Self.bomb_list[self.image_index], (SELF.X,SELF.Y)) self.image_num+=1 if Self.image_num = = 5: Self.image_num=0 Self.image_index+=1 If Self.image_index>3:time . Sleep (0.1) print ("Failure") exit () Else:self.screen.blit (Self.image, (self.x, SELF.Y)) #存放需要Deleted object Information needdelitemlist=[] for I in Self.bullet_list:if I.judge (): Needdelitemlis T.append (i) for I in NeedDelItemList:self.bullet_list.remove (i) #更新及这架飞机发射出的所有子弹的位置 #子弹移动 To determine the position of each bullet and bullet for the bullet in Self.bullet_list:bullet.display () bullet.move () def BOM B (self): Self.hit = True def moveLeft (self): self.x-=20 def moveright (self): self.x+=20 def moveUp (self): self.y-=20 def movedown (self): self.y+=20 def Fire (self): Newbullet=bullet (S Elf.x,self.y,self.screen) Self.bullet_list.append (Newbullet) class Enemyplane (Baseplane): Def __init__ (Self,scree N): baseplane.__init__ (Self,0,0,screen, "c:/users/lenovo/desktop/feiji/enemy0.png") self.direction= "right" #用来 Storage aircraft default display direction def move (self): if self.direction== "right": self.x+=2 elif self.direction== "left": self.x-=2 if (self.x>480-50): self.direction= "left" Elif (self.x<0): self.direction= ' right ' def fire (self): Random_num=random.randint (1,200) if random_num==7 or random_num==20:self.b Ullet_list.append (Enemybullet (self.x, Self.y,self.screen)) def Key_control (Heroplane): For event in Pygame.event.get ( ): if Event.type = = Quit:print ("Exit") exit () elif Event.type = = Keyd Own:if Event.key = = K_A or Event.key==k_left:print (' left ') Heroplan E.moveleft () elif Event.key = = K_d or Event.key==k_right:print (' right ') Heroplane.moveright () elif Event.key = = K_w or Event.key==k_up:print (' up ') Heroplane.moveup () elif Event.key ==k_s or Event.key==k_down:print (' Down ' ) HeroPlane.movedown () elif Event.key = = K_SPACE:heroPlane.fire () elif Eve Nt.key = = K_b:print (' B ') Heroplane.bomb () if __name__== "__main__" : Screen=pygame.display.set_mode ((480,600), 0, +) background=pygame.image.load ("c:/users/lenovo/desktop/feiji/ Background.png "). Convert () heroplane=heroplane (screen) Enemy=enemyplane (screens) while True:screen.blit (BA Ckground, (0,0)) Heroplane.display () Enemy.display () #让敌机显示 enemy.move () #调用敌机的move方法 Enemy.fire () #让敌机开火 pygame.display.update () Key_control (Heroplane) time.sleep (0.01)
For loop Pits
(prevents the list element from being deleted when it loops)
= = cannot be traversed while deleting = =
It means that you can't delete a list of your own loops, you can delete others
The For loop iterates through a list and deletes an element that has a pit.
Just point to the next element
11 22 33 Deleted 33, 44 just entered a bit (fill up), so 44 did not delete
= = Write down the person who wants to delete.
a=[11,22,33,44,55]b=[]for i in a: if i=33 or i=44: b.append(i)for i in b: a.remove(i)print(a)
Python Aircraft Wars example