Onethird_ 2021-12-02 17:15 采纳率: 100%
浏览 22
已结题

躲避雪球的小游戏 如何随机生成多个雪球

新入门python

尝试做小游戏练手

想实现以下功能:

生成世界框架 √
生成玩家 √
玩家移动方式 √
随机位置生成单个雪球 √
雪球移动方式 √

随机位置生成多个雪球 ×
积分面板 ×

两个打“×”的功能现在还没有实现
请朋友们帮忙解答一下
谢谢

import numpy as np 
import cv2 
import random

WIDTH = 20
HEIGHT = 10

player_position = 0
ice_position = [0, random.randint(0, WIDTH - 1)]

while True:
    #init world
    arr = np.zeros((HEIGHT, WIDTH)).astype(np.uint8)
    #populate player
    arr[-1, player_position] = 255
    #populate ice
    arr[ice_position[0], ice_position[1]] = 100
    #show world
    arr = cv2.resize(arr, (WIDTH * 50, HEIGHT * 50), interpolation = cv2.INTER_NEAREST)


    cv2.imshow("game", arr)  #show image
    k = cv2.waitKey(100) # wait for 100ms

    # make the ice fall
    ice_position[0] += 1

    if ice_position[0] != HEIGHT:
        arr[ice_position[0], ice_position[1]] = 100
    else:
        ice_position = [0, random.randint(0, WIDTH - 1)]
                

    # player action
    if k == ord("d"):            
        if player_position < WIDTH - 1:
            player_position += 1

    if k == ord("a"): 
        if player_position > 0:
            player_position -= 1       
        

    if k == ord("q"):
        exit()

    
    if ice_position == [HEIGHT-1, player_position]:
        exit()



  • 写回答

1条回答 默认 最新

  • 於黾 2021-12-02 17:18
    关注

    你会生成单个,那你循环一下,调用几次不就生成几个吗

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月10日
  • 已采纳回答 12月2日
  • 创建了问题 12月2日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。