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

 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. [2019杭电多校第四场][hdu6621]K-th Closest Distance(主席树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6621 题意为求区间[l,r]内第k小|a[i]-p|的值. 可以二分答案,如果二分的值为x,则判断区间 ...

  2. [BNDSOJ] #1106代码

    #include<bits/stdc++.h> using namespace std; ]; ][]; int n; bool check(int i,int j) { ]==]==]= ...

  3. 在Linux上下载和安装AAC音频编码器FAAC

    Linux上FAAC的安装 安装 下载 http://downloads.sourceforge.net/faac/faac-1.28.tar.gz 解压 tar zxvf faac-1.28.tar ...

  4. 关于JSON使用要注意的地方

    1.json     1.JSON对象;(ES5)         1)JSON.stringify() json---js[json字符串--->JavaScript对象]         2 ...

  5. CSS中如何设置父元素透明度不影响子元素透明度

    原因分析: 使用css的opcity属性改变某个元素的透明度,但是其元素下的子元素的透明度也会被改变,即便重定义也没有用,不过有个方法可以实现,大家可以看看. 可以使用一张透明的图片做背景可以达成效果 ...

  6. JavaScript ES6 Promise对象

    说明 Node.js中,以异步(Async)回调著称,使用了异步,提高了程序的执行效率,但是,代码可读性较差的. 假如有几个异步操作,后一个操作需要前一个操作的执行完毕之后返回的数据才能执行下去,如果 ...

  7. linux 服务器与客户端异常断开连接问题

    服务器与客户端连接,客户端异常断掉之后服务器端口仍然被占用, 到最后是不是服务器端达到最大连接数就没法连接了?领导让我测试这种情况,我用自己的电脑当TCP Client,虚拟机当服务器,连接之后能正常 ...

  8. 求助:关于shell数值比较的错误提示

    今天写了个脚本,过不了错误这一关,求大神路过瞟一眼. 1 #!/bin/bash 2 #disk use 3 disk_use() { 4 DISK_LOG=/tmp/disk_use.tmp 5 D ...

  9. .net core api迁移 3.0后Post 405 Method Not Allowed

    问题由来:.net core api之前是用 .net core 2.0开发的,测试过都是正常的,近期升级到了3.0,发现api get正常,post提示400,405 Method Not Allo ...

  10. C# 模拟页面登录

    using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...