如何阻止图像在列表中循环
How to stop images from looping through a list
我正在制作一款三消风格的游戏。我是 pygame 的初学者,我设法随机生成了一个棋盘,因此每次游戏都是不同的。我目前唯一的问题是我的图块似乎在我为每个图像创建的列表中不断循环。我尝试添加一个 for 循环来删除正在生成的每个元素,但我似乎没有做任何更改。有人可以帮忙吗?您可以找到我在 google 驱动器中使用的图像!我很感激任何事情!
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()
问题是每帧都会重新创建棋盘。
在应用程序循环之前生成一次电路板。清除显示并在应用程序循环中显示板子:
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()
我正在制作一款三消风格的游戏。我是 pygame 的初学者,我设法随机生成了一个棋盘,因此每次游戏都是不同的。我目前唯一的问题是我的图块似乎在我为每个图像创建的列表中不断循环。我尝试添加一个 for 循环来删除正在生成的每个元素,但我似乎没有做任何更改。有人可以帮忙吗?您可以找到我在 google 驱动器中使用的图像!我很感激任何事情!
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()
问题是每帧都会重新创建棋盘。
在应用程序循环之前生成一次电路板。清除显示并在应用程序循环中显示板子:
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()