pygame應用——生產者消費者模型
阿新 • • 發佈:2018-04-12
init 註釋 def __name__ cap pac type 消費者 屏幕
因為操作系統的一個生產者-消費者拓展作業,以一個飛機大戰的模型修改來的
1 import pygame 2 import time 3 from pygame.locals import * 4 5 bulletsNumber = 0 6 bulletStore = 20 7 #消費者類 8 class Consumer(object): 9 def __init__(self, screen): 10 # 設置飛機默認的位置 11 self.x = 600 12 self.y = 300 13 #設置要顯示內容的窗口 14 self.screen = screen 15 self.imageName = "consumer1.png" 16 self.image = pygame.image.load(self.imageName) 17 self.bulletList = [] 18 #self.bulletsNum = 0 19 20 def display(self): 21 22 # 更新飛機的位置 23 self.screen.blit(self.image, (self.x, self.y))24 needDelBullets = [] 25 for iBullet in self.bulletList: 26 if iBullet.judge(): 27 needDelBullets.append(iBullet) 28 for i in needDelBullets: 29 self.bulletList.remove(i) 30 31 #顯示所有子彈的位置 32 for bullet in self.bulletList:33 bullet.display() 34 bullet.move() 35 36 def moveLeft(self): 37 self.x -= 10 38 def moveRight(self): 39 self.x += 10 40 def moveUp(self): 41 self.y -= 10 42 def moveDown(self): 43 self.y += 10 44 def consumeBullet(self): 45 global bulletsNumber 46 if bulletsNumber > 0: 47 newBullet = Bullet(self.x,self.y,self.screen) 48 bulletsNumber -= 1 49 print("剩余子彈數:%d"%(bulletsNumber)) 50 self.bulletList.append(newBullet) 51 else: 52 print("請生產子彈!") 53 alert_image = pygame.image.load("bulletAlert.png") 54 self.screen.blit(alert_image,(-35,0)) 55 pygame.display.update() 56 time.sleep(2) 57 58 class Producer(object): 59 def __init__(self,screen): 60 self.x = 0 61 self.y = 250 62 self.screen = screen 63 self.image = pygame.image.load("producer1.png") 64 self.bulletList = [] 65 66 def display(self): 67 needDelList = [] 68 self.screen.blit(self.image,(self.x,self.y)) 69 for bullet in self.bulletList: 70 bullet.display() 71 bullet.move() 72 if bullet.judge(): 73 needDelList.append(bullet) 74 75 for i in needDelList: 76 self.bulletList.remove(i) 77 78 79 def produceBullets(self): 80 global bulletsNumber 81 global bulletStore 82 if bulletsNumber < bulletStore: 83 bulletsNumber += 1 84 newBullet = Producer_Bullets(self.x,self.y,self.screen) 85 self.bulletList.append(newBullet) 86 print("子彈數:%d"%(bulletsNumber)) 87 else: 88 bulletStoreLimit_image = pygame.image.load("bulletStoreLimit.png") 89 self.screen.blit(bulletStoreLimit_image,(-35,0)) 90 pygame.display.update() 91 time.sleep(2) 92 93 class Producer_Bullets(object): 94 def __init__(self,x,y,screen): 95 self.x = x+450 96 self.y = y+150 97 self.screen = screen 98 self.image = pygame.image.load("bullet2.png") 99 def move(self): 100 self.x += 8 101 def display(self): 102 self.screen.blit(self.image,(self.x,self.y)) 103 104 def judge(self): 105 if self.x > 680: 106 return True 107 else: 108 return False 109 110 111 class Bullet(object): 112 def __init__(self,x,y,screen): 113 self.x = x+83 114 self.y = y 115 self.screen = screen 116 self.image = pygame.image.load("bullet1.png") 117 def move(self): 118 self.y -= 10 119 120 def display(self): 121 self.screen.blit(self.image,(self.x,self.y)) 122 123 def judge(self): 124 if self.y < 0: 125 return True 126 else: 127 return False 128 129 def main(): 130 #創建一個窗體 131 screen = pygame.display.set_mode((923,609),0,32) 132 #加載背景圖片到窗體 133 background = pygame.image.load("background.png").convert() 134 pygame.display.set_caption(‘Consumer——Producer‘) 135 #創建消費者對象 136 consumer = Consumer(screen) 137 producer = Producer(screen) 138 139 140 while 1: 141 screen.blit(background,(0,0)) 142 consumer.display() 143 producer.display() 144 145 # 判斷鍵盤輸入 146 for event in pygame.event.get(): 147 # print(event.type) 148 if event.type == QUIT: 149 print("exit") 150 exit() 151 elif event.type == KEYDOWN: 152 #控制飛機移動 153 if event.key == K_a or event.key == K_LEFT: 154 print(‘left‘) 155 consumer.moveLeft() 156 elif event.key == K_d or event.key == K_RIGHT: 157 print(‘right‘) 158 consumer.moveRight() 159 elif event.key == K_w or event.key == K_UP: 160 print(‘up‘) 161 consumer.moveUp() 162 elif event.key == K_s or event.key == K_DOWN: 163 print(‘down‘) 164 consumer.moveDown() 165 elif event.key == K_SPACE: 166 print("spend bullets") 167 consumer.consumeBullet() 168 elif event.key == K_p: 169 print("produce bullets") 170 producer.produceBullets() 171 172 #更新屏幕內容 173 pygame.display.update() 174 if __name__ == ‘__main__‘: 175 main()
第一次用pygame不怎麽熟練,少了很多註釋,有什麽不明白的評論見,本人常在。
pygame應用——生產者消費者模型