调整游戏窗口大小时,滑动图像拼图游戏图像未居中

2024-06-16 14:46:43 发布

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

我正在制作一个滑动益智游戏,但当我调整游戏窗口时,左边的图像是有瓷砖的,不能与中间对齐。我从GitHub借用了这段代码。我是python新手,开始探索新事物。我想学习更多python语言,并提前向您表示感谢。以下是代码:

导入pygame作为pg 导入操作系统路径 随机输入 导入系统

类名称(): def初始化(自身): 全球基本要素

    window_width = 1380
    # window_width  =700
    window_height = 770

    self.tile_width = 150
    # self.tile_width = 75
    self.tile_height = 150
    # self.tile_height = 75

    self.coloumn = 4
    self.rows = 4

    self.img_list = [0, "image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg",
                     "image5.jpg", "image6.jpg", "image7.jpg", "image8.jpg",
                     "image9.jpg", "image10.jpg", ]

    self.empty_tile = (3, 3)
    global emptyc, emptyr
    emptyc, emptyr = 3, 3

    self.color = (255, 130, 130)
    # white = (215,215,215)
    self.yellow = (255, 255, 0)
    self.red = (200, 15, 15)
    self.black = (0, 0, 0)

    self.tiles = {}

    pg.init()
    self.gameWindow = pg.display.set_mode((window_width, window_height))
    # pg.display.set_caption("Puzzler")
    pg.display.set_caption("Fun City slide puzzle")

    # self.gameWindow.fill(white)
    self.gameWindow.fill(self.red)
    pg.display.update()

    if (os.path.isfile('level.txt')):
        lfile = open('level.txt', 'r')
        # print(storefile)
        self.level = int(lfile.read())
        # self.level=str(lfile.read())
        # print(self.highscore)
        lfile.close()
    else:
        self.level = 1

    # self.intro()
    self.start(1)

def message(self, v1, u1, text):
    rect_w = 70
    rect_h = 70

    font = pg.font.SysFont('comicsansms', 25)
    TextSurf = font.render(text, True, self.black)
    TextRect = TextSurf.get_rect()
    TextRect.center = ((v1 * rect_w + ((rect_w - 3) / 2)),
                       (u1 * rect_h + (rect_h / 2)))

    self.gameWindow.blit(TextSurf, TextRect)
    pg.display.update()

def buttons(self, text):

    # rect_w = 70
    rect_w = 180
    # rect_h = 70
    rect_h = 180
    color = self.color

    # additional button

    mouse_pos = pg.mouse.get_pos()
    click = pg.mouse.get_pressed()

    if (self.v * rect_w + rect_w - 3 > mouse_pos[0] > self.v * rect_w
            and self.u * rect_h + rect_h - 3 > mouse_pos[1] > self.u * rect_h):
        if int(text) <= self.level:
            # if str(text)<=self.level:
            color = (255, 30, 30)
            if click[0] == 1:
                self.start(int(text))
        else:
            pass

    pg.draw.rect(self.gameWindow, color, [self.v * rect_w, self.u * rect_h,
                                          rect_w - 100, rect_h - 3])
    self.message(self.v, self.u, text)
    # self.message(text)
    pg.display.update()

def intro(self):

    # additional for button
    # NEW_SURF, NEW_RECT = self.makeText("New game", self.yellow, window_width - 120, window_height - 90)

    while True:

        self.v = 4
        self.u = 5

        for event in pg.event.get():

            if event.type == pg.QUIT:
                pg.quit()
                sys.exit()

        for rec in range(1, 2):  # Level Number showing

            # self.labels(300, 430, "Tap to Start", (0, 0, 255))
            # self.buttons(str(rec))
            # self.labels()
            # self.message(self.v,self.u,str(rec))

            self.v += 1

            if self.v == 8:
                self.v = 4
                self.u += 1

#############################################################################

def labels(self, v1, u1, text, color, size=20):
    font = pg.font.SysFont('comicsansms', size)
    TextSurf = font.render(text, True, color)
    TextRect = TextSurf.get_rect()
    # print(TextRect)
    TextRect.center = (v1, u1)

    self.gameWindow.blit(TextSurf, TextRect)
    pg.display.update()

def check(self):

    global game_over

    j, k = 0, 0
    tag_list = []

    for i in range(1, 17):
        # print("checking ",i,tiles[(j,k)])

        tag = "tag" + str(i)
        # print(tag,j,k)

        if self.tiles[(j, k)][1] == tag:
            tag_list.append(tag)
            j += 1
            if j > 3:
                k += 1
                j = 0

        else:
            break

    if i == 16:
        print("GAME FINISHED")
        game_over = True

def shift(self, c, r):
    global emptyc, emptyr
    rect_color = (255, 255, 255)  # the square for suffling
    # rect_color = (0,0,0)
    self.gameWindow.blit(
        self.tiles[(c, r)][0],
        (emptyc * self.tile_width, emptyr * self.tile_height))

    '''pg.draw.rect(gameWindow,black,[c*tile_width,r*tile_height,
                              tile_width-1,tile_height-1])'''

    self.gameWindow.blit(
        self.tiles[self.empty_tile][0],
        (c * self.tile_width, r * self.tile_height))

    # state[(emptyc, emptyr)] = state[(c, r)]
    # state[(c, r)] = empty_tile

    temp = self.tiles[(c, r)]
    # print(temp,c,r)

    self.tiles[(c, r)] = self.tiles[(emptyc, emptyr)]
    self.tiles[(emptyc, emptyr)] = temp

    emptyc, emptyr = c, r

    # tiles[(emptyc, emptyr)].fill(black)
    pg.draw.rect(self.gameWindow, rect_color, [c * self.tile_width, r * self.tile_height,
                                               self.tile_width - 1, self.tile_height - 1])

    self.empty_tile = (emptyc, emptyr)

    # empty_tile.fill(0,0,0)
    pg.display.flip()

def shuffle(self):
    global emptyc, emptyr
    # keep track of last shuffling direction to avoid "undo" shuffle moves
    last_r = 0
    for i in range(100):
        # slow down shuffling for visual effect
        pg.time.delay(50)
        while True:
            # pick a random direction and make a shuffling move
            # if that is possible in that direction
            r = random.randint(1, 4)
            if (last_r + r == 5):
                # don't undo the last shuffling move
                continue
            if r == 1 and (emptyc > 0):
                self.shift(emptyc - 1, emptyr)  # shift left
            elif r == 4 and (emptyc < self.coloumn - 1):
                self.shift(emptyc + 1, emptyr)  # shift right
            elif r == 2 and (emptyr > 0):
                self.shift(emptyc, emptyr - 1)  # shift up
            elif r == 3 and (emptyr < self.rows - 1):
                self.shift(emptyc, emptyr + 1)  # shift down
            else:
                # the random shuffle move didn't fit in that direction
                continue
            last_r = r
            break  # a shuffling move was made

def start(self, l):

    f = 1
    imageX = 350
    imageY = 50
    global level, game_over
    game_over = False
    level = l
    img = self.img_list[level]
    self.image = pg.image.load("./Res/" + img)

    button = pg.image.load("./efx/" + "button.jpg")
    self.button = pg.image.load("./efx/" + "button.jpg")

    self.gameWindow.fill((190, 190, 190))  # color of the window
    for r in range(self.coloumn):

        for c in range(self.rows):

            tag = "tag" + str(f)

            tile = self.image.subsurface(c * self.tile_width, r * self.tile_height,
                                         self.tile_width - 1, self.tile_height - 1)
            f += 1
            self.tiles[(c, r)] = (tile, tag)

            if (c, r) == self.empty_tile:
                pg.draw.rect(self.gameWindow, (255, 255, 255),
                             # pg.draw.rect(self.gameWindow,(260,260,260),
                             [c * self.tile_width, r * self.tile_height,
                              self.tile_width - 1, self.tile_height - 1])#width and height of the white tile
                break

            self.gameWindow.blit(tile, (c * self.tile_width, r * self.tile_height)) # uploading the image through the window
            #self.gameWindow.blit(tile,(imageX,imageY))
            pg.display.update()
            # print(tile)

    # print(tiles)
    # text = "Level "+str(level)
    text = "Have fun!"
    self.labels(350, 625, text, (0, 0, 255))
    # self.labels(300,625,"Click to start Game",(0,0,255))
    self.labels(700, 300, "Tap to start Game", (0, 0, 255))
    self.gameWindow.blit(button, (640, 180))

    pg.display.update()

    self.gameloop()

def gameloop(self):

    started = False
    show_sol = False

    global level
    # self.gameWindow.fill((190,190,190),(150,610,300,40))

    while True:

        if game_over:
            self.labels(300, 300, "Good job well played", (255, 100, 30), 50)
            # self.labels(300,625,"Click to next Level",(0,0,255))

        for event in pg.event.get():

            # print(event)
            if event.type == pg.QUIT:
                pg.quit()
                sys.exit()

            if event.type == pg.MOUSEBUTTONDOWN:
                # print(event.type)
                # print(event.dict)
                # shuffle()

                if not started:
                    self.shuffle()
                    self.gameWindow.fill((190, 190, 190), (150, 610, 300, 40))
                    # self.labels(300,625,"Right click to see Solution",(0,0,255))
                    started = True

                if game_over:
                    level += 1
                    # self.labels(300,300,"Good job well played",(255,100,30),50)
                    # self.labels(300,625,"Click to next Level",(0,0,255))
                    if self.level < level:
                        self.level += 1
                        file = open("level.txt", "w")
                        file.write(str(self.level))
                        file.close()
                    self.start(level)

                if event.dict['button'] == 1:
                    mouse_pos = pg.mouse.get_pos()

                    c = mouse_pos[0] // self.tile_width
                    r = mouse_pos[1] // self.tile_height

                    # print("dot posn",emptyc,emptyr)
                    # print("mouse posn",c,r)

                    if c == emptyc and r == emptyr:
                        continue
                    elif c == emptyc and (r == emptyr - 1 or r == emptyr + 1):
                        self.shift(c, r)
                        self.check()
                    elif r == emptyr and (c == emptyc - 1 or c == emptyc + 1):
                        self.shift(c, r)
                        self.check()
                    # print(c,r)

                elif event.dict['button'] == 3:
                    saved_image = self.gameWindow.copy()
                    # print(saved_image)
                    # gameWindow.fill(255,255,255)
                    self.gameWindow.blit(self.image, (0, 0))
                    pg.display.flip()
                    show_sol = True

            elif show_sol and (event.type == pg.MOUSEBUTTONUP):
                # stop showing the solution
                self.gameWindow.blit(saved_image, (0, 0))
                pg.display.flip()
                show_sol = False

如果name==“main”: 拼图


Tags: textrectselfeventifwidthleveljpg
1条回答
网友
1楼 · 发布于 2024-06-16 14:46:43

^{}dest参数也可以是矩形。要在屏幕上居中显示图像,请使用^{}获取图像的边框。通过屏幕中心设置矩形的中心。使用矩形来blit图像:

game_window_rect = self.gameWindow.get_rect()
dest_rect = saved_image.get_rect(center = game_window_rect.center)
self.gameWindow.blit(saved_image, dest_rect)

相关问题 更多 >