简体   繁体   中英

Drawing the snake into my pygame game seems to not work

I am currently experiencing a problem when drawing the snake I set up in pygame.

I want to make a snake by drawing black boxes and the fruit as red boxes, But when I draw them I see different types of boxes and shapes.

I am drawing the body of the snake and the fruit only, which makes the task easier. So far I have not found a solution to my problem.

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()      

I will not answer your question here, you have far too much code for me to reasonably sift through to find your error. What I will do is make a suggestion for changing this line which makes me sad:

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

Writing this line out like this is far from the best way to do so, you can do this much quicker with a line like this:

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

This line uses what is called a list comprehension (two actually) which has many advantages such as length, readability, dependence on self.boardDimX and self.boardDimY ...

Looks like the problem lies in these types of lines:

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

You seem to misunderstand the basic concept of pygame rectangles, as it says in the Documentation , the arguments for them are:

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

By putting x2/y2 into those last two args, you are making the size proportional to the position values, which means an absurd size.

I would recommend changing these references to x2/y2 to static values, like so:

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

This will create a simple square that won't change based on coordinates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM