pygame应用——生产者消费者模型
Posted smilehang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pygame应用——生产者消费者模型相关的知识,希望对你有一定的参考价值。
因为操作系统的一个生产者-消费者拓展作业,以一个飞机大战的模型修改来的
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应用——生产者消费者模型的主要内容,如果未能解决你的问题,请参考以下文章