如何解决blit pygame 中的图像数组
我正在做一个简单的游戏,我想在随机位置显示一些带有各自矩形的 img,这是我的代码:
class Juego1:
def __init__(self,next_scene):
self.background = pygame.Surface(size)
self.imagenes1_array = ['autobus.png','coche.png','barco.png','autobus2.png','grua.png','bici.png']
for i in self.imagenes1_array:
self.imagenes1 = pygame.image.load(i)
self.imagenes1_rect = self.imagenes1.get_rect()
self.imagenes1_rect = self.imagenes1_rect.move(random.randint(0,1600),random.randint(0,900))
def start(self,gamestate):
self.gamestate = gamestate
def draw(self,screen):
self.background = pygame.Surface(size)
x = random.randint(0,1600)
y = random.randint(0,900)
coordinates = (x,y)
for i in self.imagenes1_array:
imagenes1 = pygame.image.load(i).convert()
screen.blit(imagenes1,x,y)
def update(self,events,dt):
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
if event.type == pygame.MOUSEBUTTONDOWN:
return (self.next_scene,None)
但它不断向我发送此错误: blit 的目标位置无效
我做错了什么? 另外,我不知道我想显示图像数组的 img 加载和矩形的方式很好,所以让我知道谢谢
完整代码如下:
# Importamos las librerías necesarias
import sys,pygame,pygame.freetype,random
from pygame.locals import *
size = 1600,900
# Colours R G B
GRAY = (100,100,100)
NAVYBLUE = ( 60,60,100)
BLACK = ( 0,0)
WHITE = (255,255,255)
RED = (255,0)
GREEN = ( 0,0)
BLUE = ( 0,255)
YELLOW = (255,0)
ORANGE = (255,128,0)
PURPLE = (127,255)
CYAN = ( 0,255)
class PrimeraEscena:
def __init__(self,next_scene):
self.background = pygame.Surface(size)
self.flechaimg = pygame.image.load('flecha.png')
self.flechaimg_rect = self.flechaimg.get_rect()
self.flechaimg_rect = self.flechaimg_rect.move(1300,600)
self.next_scene = next_scene
def draw(self,screen):
font = pygame.font.SysFont("comicsansms",90)
img = font.render('Memory',True,PURPLE)
screen.blit(img,(620,400))
screen.blit(self.flechaimg,(1300,600))
def update(self,dt):
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = event.pos
if self.flechaimg_rect.collidepoint(mouse_pos):
return (self.next_scene,None)
class Juego1:
def __init__(self,next_scene):
self.background = pygame.Surface(size)
self.images = []
self.rects = []
self.imagenes1_array = ['autobus.png','bici.png']
for i in self.imagenes1_array:
self.images.append(pygame.image.load(i))
s = pygame.Surface((20,20))
self.rects.append(s.get_rect())
print(self.images)
print(self.imagenes1_array)
def start(self,gamestate):
self.gamestate = gamestate
for rect in self.rects:
x = random.randint(300,1000)
y = random.randint(200,700)
rect.x = x
rect.y = y
def draw(self,screen):
self.background = pygame.Surface(size)
for i in range(len(self.images)):
screen.blit(self.images[i],(self.rects[i].x,self.rects[i].y))
def update(self,dt):
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
for rect in self.rects:
if rect.collidepoint(event.pos):
print('works!')
def main():
# Inicializamos pygame
pygame.init()
# Definimos las dimensiones de la ventana (1600 x 900px) y reloj
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
# Ponemos el título e icono y fondo de la ventana
pygame.display.set_caption("Quiz Game")
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)
fondoImg = pygame.image.load('fondo_memory.png')
dt = 0
scenes = {
'PORTADA': PrimeraEscena('SEGUNDA'),'SEGUNDA': Juego1('SEGUNDA'),}
scene = scenes['PORTADA']
# Comenzamos el bucle del juego
run=True
while run:
# RGB - Red,Green,Blue
screen.fill ((255,255))
# Imagen fondo
screen.blit(fondoImg,(0,0))
# Eventos del mouse
events = pygame.event.get()
# Capturamos los eventos que se han producido
for event in events:
# Definimos eventos:
if event.type == pygame.QUIT: # Si el evento es salir de la ventana,terminamos
run = False
result = scene.update(events,dt)
if result:
next_scene,state = result
if next_scene:
scene = scenes[next_scene]
scene.start(state)
scene.draw(screen)
pygame.display.flip()
dt = clock.tick(60)
#pygame.display.update()
if __name__ == '__main__':
main()
解决方法
第二个参数 oh pygame.Surface.blit
是一对坐标(例如元组或列表)与位置:
screen.blit(imagenes1,x,y)
screen.blit(imagenes1,(x,y))
,
您没有将加载的图像存储到列表中,您只需调用加载,分配它,然后再去。您不应该在绘图中加载图像。在 init 中将它们加载到列表中,然后在 draw 中遍历列表。
初始化:
self.images = []
self.imagenes1_array = ['autobus.png','coche.png','barco.png','autobus2.png','grua.png','bici.png']
for i in self.imagenes1_array:
self.images.append(pygame.image.load(i))
画:
def draw(self,screen):
self.background = pygame.Surface(size)
x = random.randint(0,1600)
y = random.randint(0,900)
coordinates = (x,y)
for i in self.images:
screen.blit(i,y))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。