【问题标题】:How to stop images from looping through a list如何阻止图像在列表中循环
【发布时间】:2023-03-17 18:08:01
【问题描述】:

我正在制作三消游戏。我是 pygame 的初学者,并且设法随机生成了一个棋盘,以便每个游戏都不同。目前我唯一的问题是我的图块似乎在我为每个图像创建的列表中不断循环。我尝试添加一个 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() 

【问题讨论】:

  • 我不太明白你的问题,但我看到你没有循环通过image_list,因为范围是给定的len(image_list) -6 等于零?所以你实际上并没有删除任何东西,你为什么要删除它?
  • 我最初是在考虑 xrange,直到我意识到 Python3 不做 xrange。我主要只是弄乱它来尝试让图像停止循环遍历我的所有图像文件。如果我完全取出它会更好吗?老实说,我一直在深入思考,我什至没有想过要删除它哈哈

标签: python python-3.x pygame 2d


【解决方案1】:

问题是每帧都会重新创建板子。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 2021-03-25
    • 2020-11-27
    • 2014-12-18
    相关资源
    最近更新 更多