pygame应用——生产者消费者模型
因为操作系统的一个生产者-消费者拓展作业,以一个飞机大战的模型修改来的
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应用——生产者消费者模型的更多相关文章
- 【Windows】用信号量实现生产者-消费者模型
线程并发的生产者-消费者模型: 1.两个进程对同一个内存资源进行操作,一个是生产者,一个是消费者. 2.生产者往共享内存资源填充数据,如果区域满,则等待消费者消费数据. 3.消费者从共享内存资源取数据 ...
- 第23章 java线程通信——生产者/消费者模型案例
第23章 java线程通信--生产者/消费者模型案例 1.案例: package com.rocco; /** * 生产者消费者问题,涉及到几个类 * 第一,这个问题本身就是一个类,即主类 * 第二, ...
- Java里的生产者-消费者模型(Producer and Consumer Pattern in Java)
生产者-消费者模型是多线程问题里面的经典问题,也是面试的常见问题.有如下几个常见的实现方法: 1. wait()/notify() 2. lock & condition 3. Blockin ...
- Java多线程15:Queue、BlockingQueue以及利用BlockingQueue实现生产者/消费者模型
Queue是什么 队列,是一种数据结构.除了优先级队列和LIFO队列外,队列都是以FIFO(先进先出)的方式对各个元素进行排序的.无论使用哪种排序方式,队列的头都是调用remove()或poll()移 ...
- Java多线程14:生产者/消费者模型
什么是生产者/消费者模型 一种重要的模型,基于等待/通知机制.生产者/消费者模型描述的是有一块缓冲区作为仓库,生产者可将产品放入仓库,消费者可以从仓库中取出产品,生产者/消费者模型关注的是以下几个点: ...
- Java生产者消费者模型
在Java中线程同步的经典案例,不同线程对同一个对象同时进行多线程操作,为了保持线程安全,数据结果要是我们期望的结果. 生产者-消费者模型可以很好的解释这个现象:对于公共数据data,初始值为0,多个 ...
- python_way ,day11 线程,怎么写一个多线程?,队列,生产者消费者模型,线程锁,缓存(memcache,redis)
python11 1.多线程原理 2.怎么写一个多线程? 3.队列 4.生产者消费者模型 5.线程锁 6.缓存 memcache redis 多线程原理 def f1(arg) print(arg) ...
- 如何在 Java 中正确使用 wait, notify 和 notifyAll – 以生产者消费者模型为例
wait, notify 和 notifyAll,这些在多线程中被经常用到的保留关键字,在实际开发的时候很多时候却并没有被大家重视.本文对这些关键字的使用进行了描述. 在 Java 中可以用 wait ...
- Python学习笔记——进阶篇【第九周】———线程、进程、协程篇(队列Queue和生产者消费者模型)
Python之路,进程.线程.协程篇 本节内容 进程.与线程区别 cpu运行原理 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Ev ...
随机推荐
- 工作中常见的hive语句总结
hive的启动: 1.启动hadoop2.开启 metastore 在开启 hiveserver2服务nohup hive --service metastore >> log.out 2 ...
- 【题解】Oh My Holy FFF
题目大意 有\(n\)个士兵(\(1 \leq n \leq 10^5\)),第\(i\)个士兵的身高为\(h_{i}\),现在要求把士兵按照原来的顺序分成连续的若干组,要求每组的士兵数量不超过\ ...
- Vue.js状态管理模式 Vuex
vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 安装.使用 vuex 首先我们在 vue. ...
- 成为k8s大佬,从这个操作开始(伪) - 程序员学点xx 42 k8s
目录 Kubernetes -2- 这是yann的第97篇分享 本日状态: 饿着肚子写公众号的 yann 同学. 第 1 部分 反省 昨天的内容被熊哥批评了. 熊哥说,「你光想着自己爽,一句我认为 ...
- javascript(DOM)实例
JavaScript学习笔记 JS补充笔记 实例之跑马灯,函数创建.通过ID获取标签及内部的值,字符串的获取与拼接.定时器的使用 使用定时器实现在console中打印内容 Dom选择器使用与调试记录 ...
- CSS的重用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- TCP/IP详解学习笔记(3)IP协议ARP协议和RARP协议
把这三个协议放到一起学习是因为这三个协议处于同一层,ARP协议用来找到目标主机的Ethernet网卡Mac地址,IP则承载要发送的消息.数据链路层可以从ARP得到数据的传送信息,而从IP得到要传输的数 ...
- Spark2.0基于广播变量broadcast实现实时数据按天统计
package com.gm.hive.SparkHive; import java.text.SimpleDateFormat; import java.util.Arrays; import ja ...
- python面向对象的三大特征--继承子类调用父类方法
#在子类中调用父类方法 class Vehicle: country="China" def __init__(self,name,speed,load,power): self. ...
- Ansible笔记(1)---基本概念
一.ansible的作用以及工作结构 1.1.ansible简介: ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func ...