def display_screen(action, points, input_t): # Function used to render the game screen # Get the last rendered frame global last_frame_time print("Action %s, Points: %d" % (translate_action[action], points)) # Only display the game screen if the game is not over if ("End" not in translate_action[action]): # Render the game with matplotlib plt.imshow(input_t.reshape((grid_size,) * 2), interpolation='none', cmap='gray') # Clear whatever we rendered before display.clear_output(wait=True) # And display the rendering display.display(plt.gcf()) # Update the last frame time last_frame_time = set_max_fps(last_frame_time)写成pycharm课运行的
时间: 2023-12-31 14:05:02 浏览: 28
这段代码在PyCharm中运行时需要注意几个问题:
1. 需要导入`matplotlib`和`IPython.display`库:
```python
import matplotlib.pyplot as plt
from IPython import display
```
2. 需要将`input_t`转换为`numpy`数组类型:
```python
import numpy as np
# 将 input_t 转换为 numpy 数组类型
input_t = np.array(input_t)
```
3. 在循环中调用`display_screen()`函数时,需要加入`plt.clf()`来清除之前的绘图:
```python
while not done:
# ...
display_screen(action, points, observation)
plt.clf() # 清除之前的绘图
# ...
```
综上所述,完整的代码应该是这样的:
```python
import numpy as np
import matplotlib.pyplot as plt
from IPython import display
def display_screen(action, points, input_t):
# Function used to render the game screen
# Get the last rendered frame
global last_frame_time
print("Action %s, Points: %d" % (translate_action[action], points))
# Only display the game screen if the game is not over
if ("End" not in translate_action[action]):
# Convert input_t to numpy array type
input_t = np.array(input_t)
# Render the game with matplotlib
plt.imshow(input_t.reshape((grid_size,) * 2),
interpolation='none', cmap='gray')
# Clear whatever we rendered before
display.clear_output(wait=True)
# And display the rendering
display.display(plt.gcf())
# Update the last frame time
last_frame_time = set_max_fps(last_frame_time)
# 在循环中调用 display_screen() 函数时需要加入 plt.clf() 来清除之前的绘图
while not done:
# ...
display_screen(action, points, observation)
plt.clf() # 清除之前的绘图
# ...
```
请根据您的实际情况进行调整,并确保您已经正确安装了所需的库。