简体   繁体   中英

pygame rect size not changing

import pygame

class  main():
    def __init__(self):
        self.scrX = 1000
        self.scrY = 1000
        self.win = pygame.display.set_mode((self.scrX, self.scrY))
        self.ln = 0
        self.li = 0
        self.lx = self.li + 400
        self.ly = self.ln + 400
        self.v = 1
        self.map = ["   aaaaaaa aaaaaaaaa",
                    "   aaaaaaa aa   aaaa",
                    "a  aaaaaa     a aaaa",
                    "aa        aaaaa aaaa",
                    "aaaaaaaaaaaaa   aaaa",
                    "a    aaaaaa   aaaaaa",
                    "a a   aaaa  aaa    a",
                    "a aa       aaa aaa a",
                    "a   aaaaaaaa   aaa a",
                    "aaa aaaaaaaa aaaaa a",
                    "aaa          aaaaa a",
                    "aaaaaaaaaaaaaaaaaaaa",
                                           ]

    def walls(self):
        for y in range(0, 192):
            z = 350
            if self.li == len(self.map[self.ln]):
               self.ln += 1
               self.li = 0
               print("n/")
            if ' ' in self.map[self.ln][self.li]:
                lx = self.li + z
                ly = self.ln + z
                pygame.draw.rect(self.win, (255,255,255), (lx, ly, 200, 200))
                print(".", self.ln, self.li)
                self.li += 1
            if 'a' in self.map[self.ln][self.li]:
                lx = self.li + z
                ly = self.ln + z
                pygame.draw.rect(self.win, (255,0,0), (lx, ly, 200, 200))
                print("..", self.ln, self.li)
                self.li += 1

    def mainLoop(self):
        while True:
            pygame.time.delay(10)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()

            self.win.fill((0,0,0))
            #pygame.draw.rect(self.win, (255,255,255), (self.x, self.y, 15, 15))
            main().walls()
            pygame.display.flip()
main().mainLoop()

If I change the size of the walls by editing pygame.draw.rect(self.win, (255,255,255), (lx, ly, 200, 200)) it does not change the size of the walls when I run it I have tried adding spacing to to the x and y of the walls but it doesn't change anything I also tried changing the window size but that didn't change anything either

any help would be appreciated

You have to scale the position of a wall by the size of a wall.

Define the size of a wall segment. For instance:

wall_size = 20

Scale the position of the wall by the size of a segment and draw a rectangle with the segment size:

lx = self.li * wall_size + z
ly = self.ln * wall_size + z
pygame.draw.rect(self.win, (255,255,255), (lx, ly, wall_size, wall_size))

The code can be simplified by the use of enumerate . Complete method walls :

class  main():
    # [...]

    def walls(self):
        z = 350
        wall_size = 20
        for y, line in enumerate(self.map):
            for x, ch in enumerate(line):
                lx = x * wall_size + z
                ly = y * wall_size + z
                rect = (lx, ly, wall_size, wall_size)
                if ch == ' ':
                    pygame.draw.rect(self.win, (255,255,255), rect)
                if ch == 'a':
                    pygame.draw.rect(self.win, (255,0,0), rect)

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