如何阻止图像在列表中循环

2024-04-20 02:28:19 发布

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

我正在做一个3场比赛式的游戏。我是pygame的初学者,已经设法随机生成了一个棋盘,这样每个playthrough都是不同的。目前我唯一的问题是,我的瓷砖似乎在我为每个图像创建的列表中不断循环。我尝试添加一个for循环来删除正在生成的每个元素,但我似乎没有做任何更改。有人能帮忙吗?你可以在谷歌硬盘上找到我用过的图片!我什么都感激

GoogleDrive

import pygame
from pygame.locals import *
import math
import time
import sys
import random


pygame.init()

# game variables
display_h = 900
display_w = 750
game_row = 12
game_column = 10
background_img = pygame.image.load('pink_background_resized.png')

clock = pygame.time.Clock()
running = True

# build screen and window name
screen = pygame.display.set_mode((display_w, display_h))
pygame.display.set_caption("Jelliez")
screen.fill((255,255,255))

class Tile():
    # initialize tiles
    def __init__(self, x, y, image, clicked):
        self.image = image
        self.rect = self.image.get_rect()
        self.rect.topleft = (x,y)
        self.clicked = clicked

    def draw(self):
        action = False

        # get mouse position
        pos = pygame.mouse.get_pos()

        # check mouse over and clicked psotion
        if self.rect.collidepoint(pos):
            if (pygame.mouse.get_pressed()[0] == 1) and (self.clicked == False):
                self.clicked = True
                action = True

        if pygame.mouse.get_pressed()[0] == 0:
            self.clicked = False

        
        screen.blit(self.image, (self.rect.x, self.rect.y))

        return action

def generateBoard():
    screen.blit(background_img, (0,0))
    return [[image_list[random.randrange(0, len(image_list))] for i in range (game_column)] for x in range(game_row)]

def showBoard(board):
    screen.blit(background_img, (0,0))
    rowNum = 0
    for row in board:
        columnNum = 0
        for shape in row:
            screen.blit(shape, (70 * columnNum, 64 * rowNum ))
            columnNum += 1
        rowNum += 1

# add game images
blue_jelly_img = pygame.image.load('blue_jelly.png').convert_alpha()
gray_jelly_img = pygame.image.load('gray_jelly.png').convert_alpha()
green_jelly_img = pygame.image.load('green_jelly.png').convert_alpha()
pink_jelly_img = pygame.image.load('pink_jelly.png').convert_alpha()
red_jelly_img = pygame.image.load('red_jelly.png').convert_alpha()
yellow_jelly_img = pygame.image.load('yellow_jelly.png').convert_alpha()

# create tile istances
blue_jelly = Tile(100, 231, blue_jelly_img, False)
gray_jelly = Tile(100, 231, gray_jelly_img, False)
green_jelly = Tile(100, 231, green_jelly_img, False)
pink_jelly = Tile(100, 231, pink_jelly_img, False)
red_jelly = Tile(100, 231, red_jelly_img, False)
yellow_jelly = Tile(100, 231, yellow_jelly_img, False)

image_list = [blue_jelly_img, gray_jelly_img, green_jelly_img, pink_jelly_img, red_jelly_img, yellow_jelly_img]

for x in range(len(image_list) - 6):
    del(image_list[0])
   

while running:

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

    gameBoard = generateBoard()

    showBoard(gameBoard)


    pygame.display.update()

pygame.quit() 


1条回答
网友
1楼 · 发布于 2024-04-20 02:28:19

问题是每帧都会重新创建电路板

在应用程序循环之前生成电路板一次。清除显示屏并在应用程序循环中显示电路板:

gameBoard = generateBoard()

while running:

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

    screen.blit(background_img, (0,0))
    showBoard(gameBoard)
    pygame.display.update()

pygame.quit() 

相关问题 更多 >