操作系统 多线程之吃水果 Python实现
import threading,time,pygame
background_image_filename = '/home/zhuhau/图片/bankgroup.png'
apple_image_filename = '/home/zhuhau/图片/apple.png'
orange_image_filename = '/home/zhuhau/图片/orange.png'
mom_image_filename = '/home/zhuhau/图片/mom.png'
father_image_filename = '/home/zhuhau/图片/dad.png'
son_image_filename = '/home/zhuhau/图片/儿子.png'
daughter_image_filename = '/home/zhuhau/下载/女儿.jpg'
dad_hand_image_filename = '/home/zhuhau/图片/手.png'
mom_hand_image_filename = '/home/zhuhau/图片/手.png'
son_hand_image_filename = '/home/zhuhau/图片/手.png'
daughter_hand_image_filename = '/home/zhuhau/图片/手.png'
pygame.init()
one_size = (150, 300)
two_size = (200, 300)
dad_size = (10, 10)
mom_size = (500, 10)
son_size = (10, 400)
daughter_size = (500, 400)
dad_hand_size = (80, 45)
mom_hand_size = (480, 110)
screenSize = (1000, 662)
screen = pygame.display.set_mode(screenSize, 0, 32)
pygame.display.set_caption('吃水果进程')
background = pygame.image.load(background_image_filename)
appleim = pygame.image.load(apple_image_filename)
orangeim = pygame.image.load(orange_image_filename)
mom = pygame.image.load(mom_image_filename)
father = pygame.image.load(father_image_filename)
son = pygame.image.load(son_image_filename)
daughter = pygame.image.load(daughter_image_filename)
dad_hand = pygame.image.load(dad_hand_image_filename)
mom_hand = pygame.image.load(mom_hand_image_filename)
son_hand = pygame.image.load(son_hand_image_filename)
daughter_hand = pygame.image.load(daughter_hand_image_filename)
fullscreen = False
mutex = threading.Lock()
capacity = 2
num = 0
apple = 0
orange = 0
apple_lock = threading.Semaphore(0)
orange_lock = threading.Semaphore(0)
num_lock = threading.Semaphore(2)
def daughter_eat(name):
global num, capacity,apple,orange
while True:
apple_lock.acquire()
num_lock.release()
mutex.acquire()
for i in range(50):
screen.blit(background, (0, 0))
if num == 2:
if orange == 2:
blitOne(appleim)
elif orange == 1:
blitOne(orangeim)
elif num == 1:
pass
blitPerson()
screen.blit(daughter_hand, (490 - i, 350 - i))
pygame.display.update()
apple -= 1
num -= 1
print(name, "从桌子上拿走一个苹果")
print("现在桌子上还有" + str(apple) + "苹果" + str(orange) + "橘子")
mutex.release()
time.sleep(3)
def son_eat(name):
global num, capacity, apple, orange
while True:
orange_lock.acquire()
num_lock.release()
mutex.acquire()
for i in range(50):
screen.blit(background, (0, 0))
if num == 2:
if apple == 2:
blitOne(orangeim)
elif apple == 1:
blitOne(appleim)
elif num == 1:
pass
blitPerson()
screen.blit(son_hand, (170 - i, 390 + i))
pygame.display.update()
orange -= 1
num -= 1
print(name, "从桌子上拿走一个苹果")
print("现在桌子上还有" + str(apple) + "苹果" + str(orange) + "橘子")
mutex.release()
time.sleep(3)
def father_put(name, fruit):
global num, capacity, apple, orange
while True:
num_lock.acquire()
apple_lock.release()
mutex.acquire()
print(name, "把", fruit, "放在盘子里面")
for i in range(50):
screen.blit(background, (0, 0))
if i >= 49:
if num == 0:
blitOne(appleim)
elif num == 1:
if apple == 1:
blitOne(appleim)
blitTwo(appleim)
else:
blitOne(orangeim)
blitTwo(appleim)
blitPerson()
screen.blit(dad_hand, (140 + i, 140 + i))
pygame.display.update()
apple += 1
num += 1
print("在爸爸放苹果之后,桌子上苹果的数量: " + str(apple) + " 桌子上橘子的数量: " + str(orange))
mutex.release()
time.sleep(2)
def mother_put(name, fruit):
global num, capacity, apple, orange
while True:
num_lock.acquire()
orange_lock.release()
mutex.acquire()
print(name, "把", fruit, "放在盘子里面")
for i in range(50):
screen.blit(background, (0, 0))
if i >= 49:
if num == 0:
blitOne(orangeim)
elif num == 1:
if orange == 1:
blitOne(orangeim)
blitTwo(orangeim)
else:
blitOne(appleim)
blitTwo(orangeim)
blitPerson()
screen.blit(mom_hand, (510 - i, 140 + i))
pygame.display.update()
orange += 1
num += 1
print("在妈妈放橘子之后,桌子上苹果的数量: " + str(apple) + " 桌子上橘子的数量: " + str(orange))
mutex.release()
time.sleep(2)
def blitOne(fruit):
screen.blit(fruit, one_size)
blitPerson()
def blitTwo(fruit):
screen.blit(fruit, two_size)
blitPerson()
def blitPerson():
screen.blit(father, dad_size)
screen.blit(mom, mom_size)
screen.blit(son, son_size)
screen.blit(daughter, daughter_size)
if __name__ == "__main__":
mother_thread = threading.Thread(target=mother_put, args=("妈妈", "橘子",))
father_thread = threading.Thread(target=father_put, args=("爸爸", "苹果",))
son_thread = threading.Thread(target=son_eat, args=("儿子",))
daughter_thread = threading.Thread(target=daughter_eat, args=("女儿",))
mother_thread.start()
father_thread.start()
son_thread.start()
daughter_thread.start()