把蛇引到我的游戏里似乎很有趣

2024-10-03 23:26:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前遇到了一个问题,当绘制蛇我在pygame设置。你知道吗

我想画一条蛇,把黑盒子和水果画成红盒子,但是当我画它们的时候,我看到了不同类型的盒子和形状。你知道吗

我只画蛇的身体和果实,这使任务更容易。到目前为止,我还没有找到解决我问题的办法。你知道吗

import pygame
import numpy
import random
import numpy as np
import time
import sys

class ENV:
    def __init__(self):       
        #AGENT
        self.agent = [[10,10], ]
        self.headAgent = self.agent[0]
        self.run = False

        #Move
        self.primMove = "up"
        self.directions = {
            "left" : [-1, 0],
            "right": [1, 0],
            "up": [0, 1],
            "down": [0, -1],
        }

        #Reward
        self.rewardLocationX = random.randint(1,18)
        self.rewardLocationY = random.randint(1,18)

        #BOARD
        self.boardSizeX = 500
        self.boardSizeY = 500

        self.boardDimX = 20
        self.boardDimY = 20

        self.textRep = True
        self.board = [
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
        ]

        #Game
        pygame.init()
        self.screen = pygame.display.set_mode((self.boardSizeX, self.boardSizeY))
        self.clock = pygame.time.Clock()

    def startGame(self):
        if self.run == False:
            self.run = True

        self.colorRed = (255,0,0)
        self.colorGreen = (0,255,0)
        self.colorBlue = (0,0,255)
        self.colorDarkBlue = (0,0,128)
        self.colorWhite = (255,255,255)
        self.colorYellow = (255,204,0)
        self.colorOrange = (255,100,71)
        self.colorBlack = (0,0,0)
        self.colorPink = (255,200,200)
        self.colorBgColor_black = (255,255,255)
        self.colorBgColor_white = (0,0,0)

        self.screen.fill(self.colorWhite)

    def newAction(self, action):
        reward = 0

        print(self.agent)
        if action != None:
            self.primMove = action

        direction = self.directions[self.primMove]
        a = self.agent[0][0] + direction[0]
        b = self.agent[0][1] + direction[1]    

        if action == None:
            if (a, b) == (self.rewardLocationX, self.rewardLocationY):
                self.agent.insert(0, [a,b])
                self.newReward()
                reward = 1
            else:
                self.agent.insert(0, [a,b])
                del self.agent[-1]

            self.check()

            return self.board, [self.rewardLocationX, self.rewardLocationY], self.agent, reward

        direction = self.directions[action]

        if (self.agent[0] + self.directions[action]) not in self.agent:
            if (self.agent[0] + self.directions[action]) == (self.rewardLocationX, self.rewardLocationY):
                self.agent.insert(0, [a,b])
                self.newReward()
                reward = 1
            else:
                print("DIRECTION", self.directions[action])
                self.agent.insert(0, [a,b])
                del self.agent[-1]

        else:
            print("DIRECTION", self.directions[action])
            self.agent.insert(0, [a,b])
            del self.agent[-1]

        if (a, b) == (self.rewardLocationX, self.rewardLocationY):
            self.agent.insert(0, [a,b])
            self.newReward()
            reward = 1 

        a = self.agent[0][0] + direction[0]
        b = self.agent[0][1] + direction[1]
        if [a, b] in self.agent:
            print("TOUCH OWN BODY")
            self.reset()

        print("HEAD : ", self.agent[0])
        print("REWARD : ", [self.rewardLocationX, self.rewardLocationY])

        self.check()

        self.updateBoard()
        self.printBoard()    

        return self.board, [self.rewardLocationX, self.rewardLocationY], self.agent, reward

    def reset(self):
        self.agent = [[10,10], ]
        self.headAgent = self.agent[0]
        self.run = True

        self.board = [
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
        ]

        self.updateBoard()

    def check(self):
        if self.agent[0][0] == 20 or self.agent[0][0] == -1 or self.agent[0][1] == -1 or self.agent[0][1] == 20:
            self.reset()

    def newReward(self):
        self.rewardLocationX = random.randint(1,18)
        self.rewardLocationY = random.randint(1,18)    

        self.rewardLocationX = random.randint(1,18)
        self.rewardLocationY = random.randint(1,18)

    def updateBoard(self):
        for x in range(20):
            for y in range(20):
                self.board[x][y] = " "

        for x in range(20):
            for y in range(20):
                if [x, y] in self.agent:
                    self.board[x][y] = "S"

        self.board[self.rewardLocationX][self.rewardLocationY] = "R"

    def printBoard(self):
        self.screen.fill(self.colorWhite)
        if self.textRep == True:
            for x in range(len(self.board)):
                print(self.board[x])

        for i in range(len(self.agent)):
            x1 = (self.agent[i][0] * 25) + 3
            y1 = (self.agent[i][1] * 25) + 3
            x2 = x1 + 22
            y2 = y1 + 22
            print("X1 : ", x1, "Y1 : ", y1, "X2 : ", x2, "Y2 : ", y2)
            pygame.draw.rect(self.screen, self.colorBlack, (x1, y1, x2, y2))

        print(self.rewardLocationX, self.rewardLocationY)
        x1 = (self.rewardLocationX * 25) + 3
        y1 = (self.rewardLocationY * 25) + 3
        x2 = x1 + 22
        y2 = y1 + 22
        print("REWARD   X1 : ", x1, "Y1 : ", y1, "X2 : ", x2, "Y2 : ", y2)
        pygame.draw.rect(self.screen, self.colorRed, (x1, y1, x2, y2))

        pygame.display.update()

    def makeBox(self, x, y, color):
        pygame.draw.rect(self.screen, color, ((y + 3), (x + 3), (x+22), (y+22)))

    def runGame(self):
        self.startGame()
        while self.run:
            pygame.display.update()
            self.updateBoard()
            self.printBoard()
            time.sleep(0.3)
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if (event.key == pygame.K_RIGHT):
                        print("RIGHT")
                        board, rewardLocation, agentLocation, reward = self.newAction("up")

                    if (event.key == pygame.K_LEFT):
                        print("LEFT")
                        board, rewardLocation, agentLocation, reward = self.newAction("down")

                    if (event.key == pygame.K_UP):
                        print("UP")
                        board, rewardLocation, agentLocation, reward = self.newAction("left")

                    if (event.key == pygame.K_DOWN):
                        print("DOWN")
                        board, rewardLocation, agentLocation, reward = self.newAction("right")

                    if event.key == pygame.K_ESCAPE:
                        sys.exit()
            print("PRIM")
            board, rewardLocation, agentLocation, reward = self.newAction(None)     

env = ENV()
env.runGame()      

Tags: inselfboardeventifdefactionpygame
2条回答

我不会在这里回答你的问题,你有太多的代码让我合理地筛选,以找到你的错误。我要做的是提出一个建议,改变这条让我伤心的路线:

self.board = [
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
]

这样写远不是最好的方法,你可以用这样的一行写得更快:

self.board = [[" " for i in range(self.boardDimY)]for j in range(self.boardDimY)]

这一行使用了所谓的list comprehension(实际上是两个),它有很多优点,比如长度、可读性、对self.boardDimXself.boardDimY的依赖性。。。你知道吗

问题似乎出在以下几类线路上:

pygame.draw.rect(self.screen, self.colorRed, (x1, y1, x2, y2))

您似乎误解了pygame矩形的基本概念,正如它在Documentation中所说的,它们的参数是:

Rect(left, top, width, height) -> Rect

通过将x2/y2放入最后两个参数,可以使大小与位置值成比例,这意味着一个荒谬的大小。你知道吗

我建议将这些对x2/y2的引用更改为静态值,如下所示:

pygame.draw.rect(self.screen, self.colorRed, (x1, y1, 10, 10))

这将创建一个不随坐标变化的简单正方形。你知道吗

相关问题 更多 >