import os import pygame import random import torch import sys import numpy as np import…
- User
import os import pygame import random import torch import sys import numpy as np import matplotlib.pyplot as plt from agent_cuda import DQNAgent from parameters import ( lr, gamma, epsilon, epsilon_decay, buffer_size, penalty, DEVICE, batch_size, target_update, )
RENDER = False pygame.init()
SCREEN_WIDTH = 400 SCREEN_HEIGHT = 600
RED = (255, 0, 0) WHITE = (255, 255, 255) BLACK = (0, 0, 0)
RECT_WIDTH = 35 RECT_HEIGHT = 35 PIPE_WIDTH = 70 PIPE_GAP = 220 GRAVITY = 1 JUMP_STRENGTH = -10 PIPE_SPEED = 5
class Rectangle: def init(self): self.x = SCREEN_WIDTH // 4 self.y = SCREEN_HEIGHT // 2 self.y_speed = 0
def draw(self, screen): pygame.draw.rect(screen, RED, (self.x, self.y, RECT_WIDTH, RECT_HEIGHT)) def distance(self, pipes): for pipe in pipes: if pipe.top.left > self.x: dist_horizontal = pipe.top.left - self.x gap_y_center = pipe.top.height + PIPE_GAP // 2 dist_vertically = self.y + RECT_HEIGHT // 2 - gap_y_center return dist_horizontal, dist_vertically return None, None def jump(self): self.y_speed = JUMP_STRENGTH def apply_gravity(self): self.y_speed += GRAVITY self.y += self.y_speedclass Pipe: def init(self): self.top = pygame.Rect( SCREEN_WIDTH, 0, PIPE_WIDTH, random.randint(15, SCREEN_HEIGHT - PIPE_GAP - 15), ) self.bottom = pygame.Rect( SCREEN_WIDTH, self.top.height + PIPE_GAP, PIPE_WIDTH, SCREEN_HEIGHT - self.top.height - PIPE_GAP, ) self.passed = False
def draw(self, screen): pygame.draw.rect(screen, WHITE, self.top) pygame.draw.rect(screen, WHITE, self.bottom) def move(self): self.top.x -= PIPE_SPEED self.bottom.x -= PIPE_SPEED def off_screen(self): return self.top.right < 0 def has_passed(self, rect_x): if not self.passed and self.top.right < rect_x: self.passed = True return True return Falsedef get_observation(rectangle, pipes): dist_horizontal, dist_vertically = rectangle.distance(pipes) if dist_horizontal is None or dist_vertically is None: dist_horizontal = SCREEN_WIDTH dist_vertically = SCREEN_HEIGHT // 2 normalized_horizontal = dist_horizontal / SCREEN_WIDTH normalized_vertical = dist_vertically / SCREEN_HEIGHT rect_y_normalized = rectangle.y / SCREEN_HEIGHT rect_y_speed_normalized = rectangle.y_speed / JUMP_STRENGTH observation = np.array( [ normalized_horizontal, normalized_vertical, rect_y_normalized, rect_y_speed_normalized, ] ) return observation
def take_action(rectangle, action): if action == 1: rectangle.jump()
class Environment: def init(self, render=False): self.render = render self.screen = ( pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) if self.render else None ) self.clock = pygame.time.Clock() if self.render else None if self.render: pygame.display.set_caption("Flappy Rectangle") self.font = pygame.font.SysFont(None, 36) self.reset()
- Assistant
- User
Hello from the pygame community. https://www.pygame.org/contribute.html C:\Users\Marcin\PYTHONIK\Flappy-bird-rl\visualization.py:86: FutureWarning: You are using
torch.loadwithweights_only=False(the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value forweights_onlywill be flipped toTrue. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user viatorch.serialization.add_safe_globals. We recommend you start setting for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature. Traceback (most recent call last): File "C:\Users\Marcin\PYTHONIK\Flappy-bird-rl\visualization.py", line 144, in main(model_path) File "C:\Users\Marcin\PYTHONIK\Flappy-bird-rl\visualization.py", line 96, in main action = agent.act(get_observation(rectangle, pipes), epsilon=0) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Marcin\PYTHONIK\Flappy-bird-rl\visualization.py", line 63, in get_observation dist_horizontal, dist_vertically = rectangle.distance(pipes) ^^^^^^^^^^^^^^^^^^ AttributeError: 'Rectangle' object has no attribute 'distance' - Assistant
- User
- Assistant
- User
I dont want it to train I just want to visualize the model experience in real game
- Assistant