新入门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()