Skip to content

Commit 2eb903b

Browse files
committed
Update the examples
1 parent dfc14fb commit 2eb903b

21 files changed

+88
-64
lines changed

examples/python/README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ For the examples to work properly you need to install ViZDoom or create a link t
77
### [audio_buffer.py](https://github.com/mwydmuch/ViZDoom/blob/master/examples/python/audio_buffer.py)
88
Demonstrates how to use the audio buffer.
99

10+
### [automap_buffer.py](https://github.com/mwydmuch/ViZDoom/blob/master/examples/python/automap_buffer.py)
11+
Demonstrates how to use the automap buffer.
12+
1013
### [basic.py](https://github.com/mwydmuch/ViZDoom/blob/master/examples/python/basic.py)
1114
Demonstrates how to use the most basic features of the environment. It configures the engine, and makes the agent perform random actions. It also prints the current state and the reward earned with every action.
1215

@@ -25,14 +28,14 @@ Demonstrates how to configure and play the singleplayer game for CIG competition
2528
### [delta_buttons.py](https://github.com/mwydmuch/ViZDoom/blob/master/examples/python/delta_buttons.py)
2629
Shows how delta buttons work (they may take values other than 0 and 1 and can be used for precise movement).
2730

28-
### [format.py](https://github.com/mwydmuch/ViZDoom/blob/master/examples/python/format.py)
29-
Presents different formats of the screen buffer. [OpenCV](http://opencv.org/) is used to display the images.
30-
3131
### [fps_test.py](https://github.com/mwydmuch/ViZDoom/blob/master/examples/python/fps_test.py)
3232
Tests the performance of the environment in frames per second. It should give you some idea how fast ViZDoom works on your hardware.
3333

34-
### [labels.py](https://github.com/mwydmuch/ViZDoom/blob/master/examples/python/labels.py)
35-
Contains an example of how to implement basic Q-learning on the interface within PyTorch.
34+
### [gymnasium_wrapper.py](https://github.com/mwydmuch/ViZDoom/blob/master/examples/python/gymnasium_wrapper.py)
35+
Shows how to use ViZDoom Gymnasium wrapper.
36+
37+
### [labels_buffer.py](https://github.com/mwydmuch/ViZDoom/blob/master/examples/python/labels_buffer.py)
38+
Demonstrates how to use the labels buffer.
3639

3740
### [learning_pytorch.py](https://github.com/mwydmuch/ViZDoom/blob/master/examples/python/learning_pytorch.py), [learning_pytorch_test.py](https://github.com/mwydmuch/ViZDoom/blob/master/examples/python/learning_pytorch_test.py)
3841
Contains an example of how to implement basic Q-learning on the interface within PyTorch.
@@ -58,6 +61,9 @@ Demonstrates how to record an episode and replay it.
5861
### [record_multiplayer.py](https://github.com/mwydmuch/ViZDoom/blob/master/examples/python/record_multiplayer.py)
5962
Demonstrates how to record the multiplayer game and replay it.
6063

64+
### [save_load_game.py](https://github.com/mwydmuch/ViZDoom/blob/master/examples/python/save_load_game.py)
65+
Demonstrates how to use save and load functionality build into the game engine.
66+
6167
### [scenarios.py](https://github.com/mwydmuch/ViZDoom/blob/master/examples/python/scenarios.py)
6268
Presents different scenarios that come with ViZDoom environment.
6369

examples/python/audio_buffer.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
print(
5454
"[ERROR] Could not launch ViZDoom. If you see an error above about BiquadFilter and gain,\n"
5555
" try setting game.add_game_args('+snd_efx 0'). If that fails, see\n"
56-
" https://github.com/mwydmuch/ViZDoom/pull/486"
56+
" https://github.com/Farama-Foundation/ViZDoom/pull/486"
5757
)
5858
exit(1)
5959

@@ -63,8 +63,10 @@
6363
episodes = 3
6464
audio_slices = []
6565
for i in range(episodes):
66-
print("Episode #" + str(i + 1))
66+
print(f"Episode #{i + 1}")
67+
6768
game.new_episode()
69+
6870
while not game.is_episode_finished():
6971

7072
# Gets the state
@@ -87,10 +89,10 @@
8789
if audio_data.max() == 0:
8890
print(
8991
"[WARNING] Audio buffers were full of silence. This is a common bug on e.g. Ubuntu 20.04\n"
90-
" See https://github.com/mwydmuch/ViZDoom/pull/486\n"
92+
" See https://github.com/Farama-Foundation/ViZDoom/pull/486\n"
9193
" Two possible fixes:\n"
9294
" 1) Try setting game.add_game_args('+snd_efx 0'). This my disable some audio effects\n"
93-
" 2) Try installing a newer version of OpenAL Soft library, see https://github.com/mwydmuch/ViZDoom/pull/486#issuecomment-889389185"
95+
" 2) Try installing a newer version of OpenAL Soft library, see https://github.com/Farama-Foundation/ViZDoom/pull/486#issuecomment-889389185"
9496
)
9597
# Save audio file
9698
wavfile.write("basic_sounds.wav", 22050, np.concatenate(audio_slices, axis=0))

examples/python/automap.py renamed to examples/python/automap_buffer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
sleep_time = 28
6969

7070
for i in range(episodes):
71-
print("Episode #" + str(i + 1))
71+
print(f"Episode #{i + 1}")
7272
seen_in_this_episode = set()
7373

7474
# Not needed for the first episode but the loop is nicer.
@@ -87,7 +87,7 @@
8787

8888
game.make_action(choice(actions))
8989

90-
print("State #" + str(state.number))
90+
print(f"State #{state.number}")
9191
print(
9292
"Player position X:",
9393
state.game_variables[0],

examples/python/basic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
sleep_time = 1.0 / vzd.DEFAULT_TICRATE # = 0.028
140140

141141
for i in range(episodes):
142-
print("Episode #" + str(i + 1))
142+
print(f"Episode #{i + 1}")
143143

144144
# Starts a new episode. It is not needed right after init() but it doesn't cost much. At least the loop is nicer.
145145
game.new_episode()
@@ -187,7 +187,7 @@
187187
# r = game.get_last_reward()
188188

189189
# Prints state's game variables and reward.
190-
print("State #" + str(n))
190+
print(f"State #{n}")
191191
print("Game variables:", vars)
192192
print("Reward:", r)
193193
print("=====================")

examples/python/buffers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
sleep_time = 0.028
9090

9191
for i in range(episodes):
92-
print("Episode #" + str(i + 1))
92+
print(f"Episode #{i + 1}")
9393

9494
# Not needed for the first episode but the loop is nicer.
9595
game.new_episode()
@@ -126,7 +126,7 @@
126126

127127
game.make_action(choice(actions))
128128

129-
print("State #" + str(state.number))
129+
print(f"State #{state.number}")
130130
print("=====================")
131131

132132
print("Episode finished!")

examples/python/cig_multiplayer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@
6060
frags = game.get_game_variable(vzd.GameVariable.FRAGCOUNT)
6161
if frags != last_frags:
6262
last_frags = frags
63-
print("Player " + str(player_number) + " has " + str(frags) + " frags.")
63+
print(f"Player {player_number} has {frags} frags.")
6464

6565
# Check if player is dead
6666
if game.is_player_dead():
67-
print("Player " + str(player_number) + " died.")
67+
print(f"Player {player_number} died.")
6868
# Use this to respawn immediately after death, new state will be available.
6969
game.respawn_player()
7070

examples/python/cig_multiplayer_bots.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
for i in range(episodes):
5959

60-
print("Episode #" + str(i + 1))
60+
print(f"Episode #{i + 1}")
6161

6262
# Add specific number of bots
6363
# (file examples/bots.cfg must be placed in the same directory as the Doom executable file,
@@ -79,7 +79,7 @@
7979
frags = game.get_game_variable(vzd.GameVariable.FRAGCOUNT)
8080
if frags != last_frags:
8181
last_frags = frags
82-
print("Player has " + str(frags) + " frags.")
82+
print(f"Player has {frags} frags.")
8383

8484
# Check if player is dead
8585
if game.is_player_dead():
@@ -94,11 +94,7 @@
9494
server_state = game.get_server_state()
9595
for i in range(len(server_state.players_in_game)):
9696
if server_state.players_in_game[i]:
97-
print(
98-
server_state.players_names[i]
99-
+ ": "
100-
+ str(server_state.players_frags[i])
101-
)
97+
print(f"{server_state.players_names[i]}: {server_state.players_frags[i]}")
10298
print("************************")
10399

104100
# Starts a new episode. All players have to call new_episode() in multiplayer mode.

examples/python/cig_singleplayer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
# Use this to respawn immediately after death, new state will be available.
5353
game.respawn_player()
5454

55-
print("State #" + str(state.number))
55+
print(f"State #{state.number}")
5656
print("Game variables: ", state.game_variables)
5757
print("Action:", last_action)
5858
print("Reward:", reward)

examples/python/cig_singleplayer_host.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
last_action = game.get_last_action()
9191
reward = game.get_last_reward()
9292

93-
print("State #" + str(state.number))
93+
print(f"State #{state.number}")
9494
print("Game variables: ", state.game_variables)
9595
print("Action:", last_action)
9696
print("Reward:", reward)

examples/python/delta_buttons.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
sleep_time = 0.028
4141

4242
for i in range(episodes):
43-
print("Episode #" + str(i + 1))
43+
print(f"Episode #{i + 1}")
4444

4545
game.new_episode()
4646

@@ -57,7 +57,7 @@
5757
if not time % 50:
5858
action[3] = -action[3]
5959

60-
print("State #" + str(state.number))
60+
print(f"State #{state.number}")
6161
print("Action made: ", action)
6262
print("=====================")
6363

examples/python/labels_buffer.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ def color_labels(labels):
101101
return tmp
102102

103103
for i in range(episodes):
104-
print("Episode #" + str(i + 1))
104+
print(f"Episode #{i + 1}")
105+
105106
seen_in_this_episode = set()
106107

107108
# Not needed for the first episode but the loop is nicer.
@@ -147,7 +148,7 @@ def color_labels(labels):
147148
# Make random action
148149
game.make_action(choice(actions))
149150

150-
print("State #" + str(state.number))
151+
print(f"State #{state.number}")
151152
print(
152153
"Player position: x:",
153154
state.game_variables[0],

examples/python/learning_pytorch.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def run(game, agent, actions, num_epochs, frame_repeat, steps_per_epoch=2000):
113113
game.new_episode()
114114
train_scores = []
115115
global_step = 0
116-
print("\nEpoch #" + str(epoch + 1))
116+
print(f"\nEpoch #{epoch + 1}")
117117

118118
for _ in trange(steps_per_epoch, leave=False):
119119
state = preprocess(game.get_state().screen_buffer)

examples/python/multiple_instances.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,27 @@
1111
# Run this many episodes
1212
episodes = 1
1313
config = os.path.join(vzd.scenarios_path, "multi_duel.cfg")
14+
win_x = 100
15+
win_y = 100
1416

1517

1618
def player1():
1719
game = vzd.DoomGame()
1820

1921
game.load_config(config)
22+
23+
# Setup 2 players deathmatch game that will time out after 1 minute
2024
game.add_game_args("-host 2 -deathmatch +timelimit 1 +sv_spawnfarthest 1")
21-
game.add_game_args("+name Player1 +colorset 0")
25+
26+
# Use additional arguments to set player name, color and window position
27+
game.add_game_args(f"+name Player1 +colorset 0 +win_x {win_x} +win_y {win_y}")
2228

2329
game.init()
2430

2531
actions = [[True, False, False], [False, True, False], [False, False, True]]
2632

2733
for i in range(episodes):
28-
29-
print("Episode #" + str(i + 1))
34+
print(f"Episode #{i + 1}")
3035

3136
while not game.is_episode_finished():
3237
if game.is_player_dead():
@@ -47,8 +52,14 @@ def player2():
4752
game = vzd.DoomGame()
4853

4954
game.load_config(config)
55+
56+
# Join existing game
5057
game.add_game_args("-join 127.0.0.1")
51-
game.add_game_args("+name Player2 +colorset 3")
58+
59+
# Use additional arguments to set player name, color and window position
60+
game.add_game_args(
61+
f"+name Player2 +colorset 3 +win_x {win_x + game.get_screen_width()} +win_y {win_y}"
62+
)
5263

5364
game.init()
5465

examples/python/multiple_instances_advance.py

+21-13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
episodes = 1
1818
timelimit = 1 # minutes
1919
players = 8 # number of players
20+
win_x = 100
21+
win_y = 100
2022

2123
skip = 4
2224
mode = vzd.Mode.PLAYER # or Mode.ASYNC_PLAYER
@@ -65,13 +67,14 @@ def player_action(game, player_sleep_time, actions, player_skip):
6567

6668
def player_host(p):
6769
game, actions = setup_player()
70+
71+
# Setup multiplayer deathmatch game for {p} players that will time out after {timelimit} minutes
6872
game.add_game_args(
69-
"-host "
70-
+ str(p)
71-
+ " -netmode 0 -deathmatch +timelimit "
72-
+ str(timelimit)
73-
+ " +sv_spawnfarthest 1 +name Player0 +colorset 0"
73+
f"-host {p} -netmode 0 -deathmatch +timelimit {timelimit} +sv_spawnfarthest 1"
7474
)
75+
# Use additional arguments to set player name, color and window position
76+
game.add_game_args(f"+name Player0 +colorset 0 +win_x {win_x} +win_y {win_y}")
77+
# Add additional arguments
7578
game.add_game_args(args)
7679

7780
game.init()
@@ -81,7 +84,7 @@ def player_host(p):
8184
player_skip = skip
8285

8386
for i in range(episodes):
84-
print("Episode #" + str(i + 1))
87+
print(f"Episode #{i + 1}")
8588
episode_start_time = None
8689

8790
while not game.is_episode_finished():
@@ -100,10 +103,8 @@ def player_host(p):
100103
player_count = int(game.get_game_variable(vzd.GameVariable.PLAYER_COUNT))
101104
for i in range(1, player_count + 1):
102105
print(
103-
"Host: Player" + str(i) + ":",
104-
game.get_game_variable(
105-
eval("vzd.GameVariable.PLAYER" + str(i) + "_FRAGCOUNT")
106-
),
106+
f"Host: Player{i}:",
107+
game.get_game_variable(eval(f"vzd.GameVariable.PLAYER{i}_FRAGCOUNT")),
107108
)
108109
print("Host: Episode processing time:", time() - episode_start_time)
109110

@@ -115,7 +116,14 @@ def player_host(p):
115116

116117
def player_join(p):
117118
game, actions = setup_player()
118-
game.add_game_args("-join 127.0.0.1 +name Player" + str(p) + " +colorset " + str(p))
119+
120+
# Join existing game
121+
game.add_game_args("-join 127.0.0.1")
122+
# Use additional arguments to set player name, color and window position
123+
game.add_game_args(
124+
f"+name Player{p} +colorset 0 +win_x {win_x + p % 4 * game.get_screen_width()} +win_y {win_y + p // 4 * game.get_screen_height()} "
125+
)
126+
# Add additional arguments
119127
game.add_game_args(args)
120128

121129
game.init()
@@ -129,7 +137,7 @@ def player_join(p):
129137
while not game.is_episode_finished():
130138
state = game.get_state()
131139
print(
132-
"Player" + str(p) + ":",
140+
f"Player{p}:",
133141
state.number,
134142
action_count,
135143
game.get_episode_time(),
@@ -138,7 +146,7 @@ def player_join(p):
138146
action_count += 1
139147

140148
print(
141-
"Player" + str(p) + " frags:",
149+
f"Player{p} frags:",
142150
game.get_game_variable(vzd.GameVariable.FRAGCOUNT),
143151
)
144152
game.new_episode()

examples/python/objects_and_sectors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
sleep_time = 1.0 / vzd.DEFAULT_TICRATE # = 0.028
6262

6363
for i in range(episodes):
64-
print("Episode #" + str(i + 1))
64+
print(f"Episode #{i + 1}")
6565

6666
# Not needed for the first episode but the loop is nicer.
6767
game.new_episode()
@@ -71,7 +71,7 @@
7171
state = game.get_state()
7272
game.make_action(choice(actions))
7373

74-
print("State #" + str(state.number))
74+
print(f"State #{state.number}")
7575
print(
7676
"Player position: x:",
7777
state.game_variables[0],

examples/python/pyoblige.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
last_action = game.get_last_action()
117117
reward = game.get_last_reward()
118118

119-
print("State #" + str(state.number))
119+
print(f"State #{state.number}")
120120
print("Game variables: ", state.game_variables)
121121
print("Action:", last_action)
122122
print("Reward:", reward)

0 commit comments

Comments
 (0)