操作系统 多线程之吃水果 Python实现

操作系统 多线程之吃水果 Python实现

import threading,time,pygame

# 传入文件的 URL 注意此处的 URL 必须为绝对路径,否则会报错
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,为使用硬件做准备
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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值