I have code that generates enemy movements, and a neural network that learns to hit the…
- User
I have code that generates enemy movements, and a neural network that learns to hit the enemy, it takes into account the speed of the bullet, predicts the position of the enemy when the bullet ends its flight, and so on.
but the problem is that I can't manually generate player movements, bullet types, and even ballistics. what advice can you give when developing new features, and what code can you write me for this? it would be nice to have visualization to simplify development, it should be in real time, because I will integrate the trained neural network into the android emulator into the game. Or are other methods needed? write code if needed.
import numpy as np import random import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense
Constants
GAME_AREA_WIDTH = 1000 GAME_AREA_HEIGHT = 1000
Random enemy movement
def update_enemy_position(enemy_pos, enemy_vel): new_pos_x = enemy_pos[0] + enemy_vel[0] new_pos_y = enemy_pos[1] + enemy_vel[1] return new_pos_x, new_pos_y
def random_velocity(): speed = random.uniform(1, 40) angle = random.uniform(0, 2 * np.pi) vel_x = speed * np.cos(angle) vel_y = speed * np.sin(angle) return vel_x, vel_y
Neural network
input_neurons = 9 # Including the enemy velocities (2 additional inputs) output_neurons = 2
model = Sequential() model.add(Dense(32, activation='relu', input_dim=input_neurons)) model.add(Dense(64, activation='relu')) model.add(Dense(output_neurons))
model.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
Input: bullet speed, bullet range, player x, player y, enemy x, enemy y, enemy velocity x, enemy velocity y
def desired_joystick_coords(player_pos, enemy_pos, enemy_vel, bullet_speed, bullet_range): time_to_hit = bullet_range / bullet_speed
future_enemy_pos = enemy_pos for _ in range(int(time_to_hit)): future_enemy_pos = update_enemy_position(future_enemy_pos, enemy_vel) enemy_vel = random_velocity() # Update enemy velocity to create unpredictable movements joystick_x = future_enemy_pos[0] - player_pos[0] joystick_y = future_enemy_pos[1] - player_pos[1] return joystick_x, joystick_ydef generate_training_data(num_samples): training_data = []
for _ in range(num_samples): bullet_speed = random.uniform(500, 10000) bullet_range = random.uniform(2000, 20000) player_pos = (random.randint(0, GAME_AREA_WIDTH), random.randint(0, GAME_AREA_HEIGHT)) enemy_pos = (random.randint(0, GAME_AREA_WIDTH), random.randint(0, GAME_AREA_HEIGHT)) enemy_vel = random_velocity() step = random.randint(0, 100) desired_coords = desired_joystick_coords(player_pos, enemy_pos, enemy_vel, bullet_speed, bullet_range) input_data = (bullet_speed, bullet_range, player_pos[0], player_pos[1], enemy_pos[0], enemy_pos[1], enemy_vel[0], enemy_vel[1], step) output_data = desired_coords training_data.append((input_data, output_data)) return training_data - Assistant