因为操作系统的一个生产者-消费者拓展作业,以一个飞机大战的模型修改来的

 import pygame
import time
from pygame.locals import * bulletsNumber = 0
bulletStore = 20
#消费者类
class Consumer(object):
def __init__(self, screen):
# 设置飞机默认的位置
self.x = 600
self.y = 300
# 设置要显示内容的窗口
self.screen = screen
self.imageName = "consumer1.png"
self.image = pygame.image.load(self.imageName)
self.bulletList = []
#self.bulletsNum = 0 def display(self): # 更新飞机的位置
self.screen.blit(self.image, (self.x, self.y))
needDelBullets = []
for iBullet in self.bulletList:
if iBullet.judge():
needDelBullets.append(iBullet)
for i in needDelBullets:
self.bulletList.remove(i) #显示所有子弹的位置
for bullet in self.bulletList:
bullet.display()
bullet.move() def moveLeft(self):
self.x -= 10
def moveRight(self):
self.x += 10
def moveUp(self):
self.y -= 10
def moveDown(self):
self.y += 10
def consumeBullet(self):
global bulletsNumber
if bulletsNumber > 0:
newBullet = Bullet(self.x,self.y,self.screen)
bulletsNumber -= 1
print("剩余子弹数:%d"%(bulletsNumber))
self.bulletList.append(newBullet)
else:
print("请生产子弹!")
alert_image = pygame.image.load("bulletAlert.png")
self.screen.blit(alert_image,(-35,0))
pygame.display.update()
time.sleep(2) class Producer(object):
def __init__(self,screen):
self.x = 0
self.y = 250
self.screen = screen
self.image = pygame.image.load("producer1.png")
self.bulletList = [] def display(self):
needDelList = []
self.screen.blit(self.image,(self.x,self.y))
for bullet in self.bulletList:
bullet.display()
bullet.move()
if bullet.judge():
needDelList.append(bullet) for i in needDelList:
self.bulletList.remove(i) def produceBullets(self):
global bulletsNumber
global bulletStore
if bulletsNumber < bulletStore:
bulletsNumber += 1
newBullet = Producer_Bullets(self.x,self.y,self.screen)
self.bulletList.append(newBullet)
print("子弹数:%d"%(bulletsNumber))
else:
bulletStoreLimit_image = pygame.image.load("bulletStoreLimit.png")
self.screen.blit(bulletStoreLimit_image,(-35,0))
pygame.display.update()
time.sleep(2) class Producer_Bullets(object):
def __init__(self,x,y,screen):
self.x = x+450
self.y = y+150
self.screen = screen
self.image = pygame.image.load("bullet2.png")
def move(self):
self.x += 8
def display(self):
self.screen.blit(self.image,(self.x,self.y)) def judge(self):
if self.x > 680:
return True
else:
return False class Bullet(object):
def __init__(self,x,y,screen):
self.x = x+83
self.y = y
self.screen = screen
self.image = pygame.image.load("bullet1.png")
def move(self):
self.y -= 10 def display(self):
self.screen.blit(self.image,(self.x,self.y)) def judge(self):
if self.y < 0:
return True
else:
return False def main():
#创建一个窗体
screen = pygame.display.set_mode((923,609),0,32)
#加载背景图片到窗体
background = pygame.image.load("background.png").convert()
pygame.display.set_caption('Consumer——Producer')
#创建消费者对象
consumer = Consumer(screen)
producer = Producer(screen) while 1:
screen.blit(background,(0,0))
consumer.display()
producer.display() # 判断键盘输入
for event in pygame.event.get():
# print(event.type)
if event.type == QUIT:
print("exit")
exit()
elif event.type == KEYDOWN:
#控制飞机移动
if event.key == K_a or event.key == K_LEFT:
print('left')
consumer.moveLeft()
elif event.key == K_d or event.key == K_RIGHT:
print('right')
consumer.moveRight()
elif event.key == K_w or event.key == K_UP:
print('up')
consumer.moveUp()
elif event.key == K_s or event.key == K_DOWN:
print('down')
consumer.moveDown()
elif event.key == K_SPACE:
print("spend bullets")
consumer.consumeBullet()
elif event.key == K_p:
print("produce bullets")
producer.produceBullets() #更新屏幕内容
pygame.display.update()
if __name__ == '__main__':
main()

第一次用pygame不怎么熟练,少了很多注释,有什么不明白的评论见,本人常在。

附送一个python程序打包教程:

Python程序打包为可执行文件exe

  

pygame应用——生产者消费者模型的更多相关文章

  1. 【Windows】用信号量实现生产者-消费者模型

    线程并发的生产者-消费者模型: 1.两个进程对同一个内存资源进行操作,一个是生产者,一个是消费者. 2.生产者往共享内存资源填充数据,如果区域满,则等待消费者消费数据. 3.消费者从共享内存资源取数据 ...

  2. 第23章 java线程通信——生产者/消费者模型案例

    第23章 java线程通信--生产者/消费者模型案例 1.案例: package com.rocco; /** * 生产者消费者问题,涉及到几个类 * 第一,这个问题本身就是一个类,即主类 * 第二, ...

  3. Java里的生产者-消费者模型(Producer and Consumer Pattern in Java)

    生产者-消费者模型是多线程问题里面的经典问题,也是面试的常见问题.有如下几个常见的实现方法: 1. wait()/notify() 2. lock & condition 3. Blockin ...

  4. Java多线程15:Queue、BlockingQueue以及利用BlockingQueue实现生产者/消费者模型

    Queue是什么 队列,是一种数据结构.除了优先级队列和LIFO队列外,队列都是以FIFO(先进先出)的方式对各个元素进行排序的.无论使用哪种排序方式,队列的头都是调用remove()或poll()移 ...

  5. Java多线程14:生产者/消费者模型

    什么是生产者/消费者模型 一种重要的模型,基于等待/通知机制.生产者/消费者模型描述的是有一块缓冲区作为仓库,生产者可将产品放入仓库,消费者可以从仓库中取出产品,生产者/消费者模型关注的是以下几个点: ...

  6. Java生产者消费者模型

    在Java中线程同步的经典案例,不同线程对同一个对象同时进行多线程操作,为了保持线程安全,数据结果要是我们期望的结果. 生产者-消费者模型可以很好的解释这个现象:对于公共数据data,初始值为0,多个 ...

  7. python_way ,day11 线程,怎么写一个多线程?,队列,生产者消费者模型,线程锁,缓存(memcache,redis)

    python11 1.多线程原理 2.怎么写一个多线程? 3.队列 4.生产者消费者模型 5.线程锁 6.缓存 memcache redis 多线程原理 def f1(arg) print(arg) ...

  8. 如何在 Java 中正确使用 wait, notify 和 notifyAll – 以生产者消费者模型为例

    wait, notify 和 notifyAll,这些在多线程中被经常用到的保留关键字,在实际开发的时候很多时候却并没有被大家重视.本文对这些关键字的使用进行了描述. 在 Java 中可以用 wait ...

  9. Python学习笔记——进阶篇【第九周】———线程、进程、协程篇(队列Queue和生产者消费者模型)

    Python之路,进程.线程.协程篇 本节内容 进程.与线程区别 cpu运行原理 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Ev ...

随机推荐

  1. pip源地址

    pip国内的一些镜像   阿里云 http://mirrors.aliyun.com/pypi/simple/   中国科技大学 https://pypi.mirrors.ustc.edu.cn/si ...

  2. poj2019 二维RMQ裸题

    Cornfields Time Limit: 1000MS   Memory Limit: 30000K Total Submissions:8623   Accepted: 4100 Descrip ...

  3. 搜索(BFS)---最短单词路径

    最短单词路径 127. Word Ladder (Medium) Input: beginWord = "hit", endWord = "cog", word ...

  4. 【JAVA】增强for循环for(int a : arr)

    介绍 这种有冒号的for循环叫做foreach循环,foreach语句是java5的新特征之一,在遍历数组.集合方面,foreach为开发人员提供了极大的方便. foreach语句是for语句的特殊简 ...

  5. 【错误】Publishing to Tomcat'has encountered a problem

    tomcat 启动工程时候出现 Publishing to Tomcat'has encountered a problem错误 解决方案 之后重启tomcat 就可以正常启动了

  6. FM

    1.FM (因子分解机) 2.FM的作用: (1)特征组合是许多机器学习建模过程中遇到的问题,如果对特征直接进行建模,很可能会忽略掉特征与特征之间的关联信息,因此,可以通过构建新的交叉特征这一特征组合 ...

  7. $2019$各种$WC$没去记

    \(2019\)各种\(WC\)没去记 太弱了去不了啊. 至少我联赛没退役是吧...(退役感++ 不过这个分数线还是有点让人自闭啊,划线人绝对有毒,有人关照一下空巢老人\(mona\)喵? 这里大概是 ...

  8. [POJ 1390] Blocking

    问题描述 Some of you may have played a game called 'Blocks'. There are n blocks in a row, each box has a ...

  9. 如何Docker化任意一个应用

    网上有很多关于如何将应用Docker化的教程,为什么我还要再写一个呢? 我见过的大部分教程都是限定在某种特定技术(例如Java或者Python),可能无法满足读者的需求.同时,这些教程也没有说清楚关于 ...

  10. CSS中的背景用法详解

    background 属性是CSS中用于设置元素背景的属性,最简单的background属 性名,是针对背景若干设定的合并简写,最早的CSS只能使用单一背景图片,而在现在却可以设置多个背景图片.而不用 ...