oldxacorn 2020-06-05 19:32 采纳率: 0%
浏览 381

pygame移动方块碰撞设定边界问题


```import math
import sys
import numpy as np
import pygame


class Maze(object):
    state_dim = 4
    action_dim = 1
    action_bound = [0, 2 * math.pi]

    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((600, 500))
        # self.a = np.random.random_sample() * (2 * math.pi)        # action 取值范围[0, 2π]
        self.env = pygame.draw.rect(self.screen, (0, 0, 0), (45, 30, 500, 450), 2)
        # 初始化agent的信息
        self.agent_pos_x = 48
        self.agent_pos_y = 34
        self.v = 0.7
        self.vel_x = 0
        self.vel_y = 0
        self.agent_pos = self.agent_pos_x, self.agent_pos_y, 50, 45

        #  start point
        self.start_pos_x = 48
        self.start_pos_y = 34
        self.start_pos = self.start_pos_x, self.start_pos_y, 50, 45

        # hell位置信息
        self.hell_pos_x = 300  # hell initial position
        self.hell_pos_y = 250
        self.vel_xh = 0  # .1 * np.random.random_sample()  # make hell move
        self.vel_yh = 0  # .1 * np.random.random_sample()
        #  hell point
        self.hell_pos_x += self.vel_xh
        self.hell_pos_y += self.vel_yh
        self.hell_pos = self.hell_pos_x, self.hell_pos_y, 50, 45

        if self.hell_pos_x > 490 or self.hell_pos_x < 48:
            self.vel_xh = -self.vel_xh
        if self.hell_pos_y > 430 or self.hell_pos_y < 34:
            self.vel_yh = -self.vel_yh
        #  end point
        self.end_pos_x = 490
        self.end_pos_y = 430
        self.end_pos = self.end_pos_x, self.end_pos_y, 50, 45
        # self.end = pygame.draw.rect(self.screen, (255, 0, 0), self.end_pos, 0)

        # pygame.display.set_caption("Simple Moving")
        ###########################################################################################

    def step(self, action):
        done = False
        # action = np.clip(action, a_min=0, a_max=2 * math.pi)
        action = np.clip(action, *self.action_bound)
        self.vel_x = self.v * math.cos(action)
        self.vel_y = self.v * math.sin(action)
        self.agent_pos_x = self.agent_pos_x + self.vel_x
        self.agent_pos_y = self.agent_pos_y + self.vel_y

        # def colliderect(self):
        # self.agent = pygame.draw.rect(self.screen, (255, 255, 0), (self.agent_pos_x, self.agent_pos_y, 50, 45), 0)
        # normalize features
        dist1 = [(self.end_pos_x - self.agent_pos_x), (self.end_pos_y - self.agent_pos_y)]

        if self.agent.colliderect(self.hell):  # 智能体与障碍物是否相碰
            reward = -1
            print(reward)
            print("misson failed")
            done = True
        elif self.agent.colliderect(self.end):
            reward = 1
            print("victory")
            done = True
        else:
            reward = 0
            done = False
        s = np.concatenate(([self.agent_pos_x, self.agent_pos_y], dist1))
        if done:
            pygame.quit()
            sys.exit()
        return s, reward, done

    def outside(self):
        if self.agent_pos_y > 490 or self.agent_pos_y < 48:  # 碰到边界线反弹
            self.vel_x = -self.vel_x
        if self.agent_pos_y > 430 or self.agent_pos_y < 34:
            self.vel_y = -self.vel_y

    def reset(self):
        self.agent_pos_x = 48
        self.agent_pos_y = 34
        self.vel_x = 0
        self.vel_y = 0
        self.agent_pos = self.agent_pos_x, self.agent_pos_y, 50, 45
        self.agent = pygame.draw.rect(self.screen, (255, 255, 0), (self.agent_pos_x, self.agent_pos_y, 50, 45), 0)
        dist1 = [(self.end_pos_x - self.agent_pos_x), (self.end_pos_y - self.agent_pos_y)]
        s = np.concatenate(([self.agent_pos_x, self.agent_pos_y], dist1))
        return s

    def render(self):
        self.screen = pygame.display.set_mode((600, 500))
        self.screen.fill((255, 255, 255))
        pygame.draw.rect(self.screen, (0, 0, 0), (45, 30, 500, 450), 2)
        pygame.draw.rect(self.screen, (0, 0, 0), self.start_pos, 2)
        self.hell = pygame.draw.rect(self.screen, (0, 0, 0), self.hell_pos, 0)
        self.end = pygame.draw.rect(self.screen, (255, 0, 0), self.end_pos, 0)
        self.agent = pygame.draw.rect(self.screen, (255, 255, 0), (self.agent_pos_x, self.agent_pos_y, 50, 45), 0)
        pygame.display.flip()

    def sample_action(self):
        return np.random.random_sample() * (2 * math.pi)  # action 取值范围[0, 2π]

    # def run(self):
    #     env = Maze()
    #     ''''''''''''''''''''''''''
    #     while True:
    #         for event in pygame.event.get():
    #             if event.type == pygame.QUIT:
    #                 pygame.quit()
    #                 exit(0)
    #         env.render()
    #         env.step(env.sample_action())
    #         # if env.colliderect() is True:
    #         # env.reset()


if __name__ == '__main__':
    env = Maze()
    ''''''''''''''''''''''''''
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit(0)
        env.render()
        env.outside()
        env.step(env.sample_action())

1. ****
网上大多碰到边缘都是按照我写的outside进行的,但是我的怎么运行还是会出边界,求解
  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2020-09-03 09:38
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!