1.def show_gameover_screen():
2. gameDisplay.blit(background, (0,0))
3. draw_text(gameDisplay, "FRUIT NINJA!", 90, WIDTH / 2, HEIGHT / 4)
4. if not game_over :
5. draw_text(gameDisplay,"Score : " + str(score), 50, WIDTH / 2, HEIGHT /2)
6.
7. draw_text(gameDisplay, "Press a key to begin!", 64, WIDTH / 2, HEIGHT * 3 / 4)
8. pygame.display.flip()
9. waiting = True
10. while waiting:
11. clock.tick(FPS)
12. for event in pygame.event.get():
13. if event.type == pygame.QUIT:
14. pygame.quit()
15. if event.type == pygame.KEYUP:
16. waiting = False
•show_gameover_screen()函数显示初始游戏画面和游戏结束画面。
•pygame.display.flip()将只更新屏幕的一部分,但如果没有参数传递,则会更新整个屏幕。
•pygame.event.get()将返回存储在pygame事件队列中的所有事件。
•如果事件类型等于quit,那么pygame将退出。
•event.KEYUP事件,当按键被按下和释放时发生的事件。
# -*- coding: utf-8 -*-
# 创建两个列表
fruits = [
{'name': '猕猴桃', 'quantity': 125},
{'name': '苹果', 'quantity': 315},
{'name': '香蕉', 'quantity': 80},
{'name': '草莓', 'quantity': 50},
{'name': '葡萄', 'quantity': 70},
{'name': '砂糖橘', 'quantity': 200}
]
prices = [
{'name': '猕猴桃', 'price': 15},
{'name': '苹果', 'price': 8},
{'name': '香蕉', 'price': 5},
{'name': '草莓', 'price': 30},
{'name': '葡萄', 'price': 12},
{'name': '砂糖橘', 'price': 6}
]
# 合并两个列表
length = len(fruits)
merged_fruits = [] # 存储当前的库存信息
for idx, fruit in enumerate(fruits):
merged_fruits.append({**fruit, **prices[idx]})
print("#################################")
print(f"合并后的列表如下:\n{merged_fruits}")
print("#################################")
# 输出quantity < 100的水果列表
small_inv_fruits = []
for fruit in merged_fruits:
if fruit["quantity"] < 100:
small_inv_fruits.append(fruit)
print("#################################")
print(f"库存小于100的水果列表如下:\n{small_inv_fruits}")
print("#################################")
sorted_fruits = sorted(merged_fruits, key=lambda f: f.get("quantity"))
sorted_fruits.reverse() # 降序排列
# 根据水果数量降序输出水果的名称,数量和单价
print("#################################")
print("根据水果数量降序输出水果的名称,数量和单价:")
for fruit in sorted_fruits:
print(f"名称:{fruit.get('name')} 数量:{fruit.get('quantity')} 单价:{fruit.get('price')}")
print("#################################")
purchase_fruits = {
'猕猴桃': 5,
'草莓': 2,
'砂糖橘': 5
}
print("#################################")
print("更新库存:")
for fruit in merged_fruits:
amount = purchase_fruits.get(fruit['name'], 0)
fruit['quantity'] -= amount
print(f"最新库存为: {[f['name'] + ':' + str(f['quantity']) for f in merged_fruits]}")
print("计算金额:")
total = 0
for fruit in merged_fruits:
name, price = fruit['name'], fruit['price']
amount = purchase_fruits.get(name, 0)
if amount > 0:
price = price * 0.9 if price > 10 else price
total += amount * price
print(f"金额为:{total}")
print("#################################")