| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 
 | import pygameimport time
 from pygame.locals import *
 import random
 
 class Base(object):
 def __init__(self,screen_temp,x,y,image_name):
 self.x = x
 self.y = y
 self.screen = screen_temp
 self.image = pygame.image.load(image_name)
 
 
 class BasePlane(Base):
 
 def __init__(self,screen_temp,x,y,image_name):
 Base.__init__(self,screen_temp,x,y,image_name)
 self.bullet_list = []
 
 def display(self):
 self.screen.blit(self.image, (self.x,self.y))
 for bullet in self.bullet_list:
 bullet.display()
 bullet.move()
 if bullet.judge():
 self.bullet_list.remove(bullet)
 
 
 class HeroPlane(BasePlane):
 
 def __init__(self,screen_temp):
 BasePlane.__init__(self,screen_temp,210,720,"./images/hero1.png")
 
 def move_left(self):
 self.x -= 5
 
 def move_right(self):
 self.x += 5
 
 def fire(self):
 self.bullet_list.append(Bullet(self.screen,self.x,self.y))
 
 class EnemyPlane(BasePlane):
 """敌机"""
 def __init__(self,screen_temp):
 BasePlane.__init__(self, screen_temp, 0, 0, "./images/enemy0.png")
 self.direction = 'right'
 
 def move(self):
 if self.direction == 'right':
 self.x += 5
 elif self.direction == 'left':
 self.x -= 5
 
 if self.x > 480 - 50:
 self.direction = 'left'
 elif self.x < 0:
 self.direction = 'right'
 
 
 def fire(self):
 random_num = random.randint(1,100)
 if  random_num == 8 or random_num == 20:
 self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y))
 
 
 class BaseBullet(Base):
 def __init__(self,screen_temp,x,y,image_name):
 Base.__init__(self, screen_temp, x, y, image_name)
 
 def display(self):
 self.screen.blit(self.image,(self.x,self.y))
 
 class Bullet(BaseBullet):
 
 def __init__(self,screen_temp,x,y):
 BaseBullet.__init__(self,screen_temp,x + 40,y -20,"./images/bullet.png")
 
 def move(self):
 self.y -= 5
 
 def judge(self):
 if self.y < 0:
 return True
 else:
 return False
 
 
 class EnemyBullet(BaseBullet):
 
 def __init__(self,screen_temp,x,y):
 BaseBullet.__init__(self, screen_temp, x + 25, y + 40, "./images/bullet1.png")
 
 def move(self):
 self.y += 5
 
 def judge(self):
 if self.y > 852:
 return True
 else:
 return False
 
 
 def key_control(hero_temp):
 
 for event in pygame.event.get():
 
 if event.type == QUIT:
 print("exit")
 exit()
 
 
 if event.type == KEYDOWN:
 
 if event.key == K_a or event.key == K_LEFT:
 print("left")
 hero_temp.move_left()
 
 elif event.key == K_d or event.key == K_RIGHT:
 print("right")
 hero_temp.move_right()
 
 elif event.key == K_SPACE:
 print("space")
 hero_temp.fire()
 
 def main():
 
 screen =  pygame.display.set_mode((480,852),0,32)
 
 background = pygame.image.load("./images/background.png")
 
 hero = HeroPlane(screen)
 
 enemy = EnemyPlane(screen)
 
 
 while True:
 
 screen.blit(background,(0,0))
 hero.display()
 enemy.display()
 enemy.move()
 enemy.fire()
 
 
 pygame.display.update()
 
 
 key_control(hero)
 
 time.sleep(0.01)
 
 
 if __name__ == '__main__':
 main()
 
 |