本人刚开始学习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] !=10:
arr[ice_position[0], ice_position[1]] = 100
else:
ice_position[0] = 0
# 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()