Boa tarde a todos, o que há de errado no meu código abaixo?? porque estou tentando fazer aparecer o sprite que têm as imagens do personagem andando para direita, esquerda, para cima e para baixo, mas dá erro:
Traceback (most recent call last):
File "/home/fabio/Projetos/Turismo3D/Source/Game.py", line 141, in
class Game():
File "/home/fabio/Projetos/Turismo3D/Source/Game.py", line 227, in Game
screen.blit(av, av_pos)
TypeError: invalid destination position for blit
Script terminated.
Segue abaixo o script
import pygame, sys, os, math
from pygame.locals import *
# initializing pygame
pygame.init()
# setting up the screen
pygame.display.set_caption('Cenario')
size = width, height = 800, 600
screen = pygame.display.set_mode((800, 600), RESIZABLE, 32)
black = 0, 0, 0
window = pygame.display.set_mode((800, 600), RESIZABLE, 32)
# fliping the display
pygame.display.flip()
# ############################################################################
# def load_image #
# 02/09/2008 #
# #
# function that loads the images into memory #
# ############################################################################
def load_image(name):
fullname = os.path.join('imagens', name)
try:
image = pygame.image.load(fullname).convert_alpha()
except pygame.error, message:
print 'Cannot load image:', fullname
raise SystemExit, message
return image, image.get_rect()
# end load_image
# ############################################################################
# class Vector2 #
# 02/16/2008 #
# #
# class responsible for the creation of vectors for moving images in 2D #
# ############################################################################
class Vector2(object):
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
def __str__(self):
return "(%s, %s)"%(self.x, self.y)
@staticmethod
def from_points(P1, P2):
return Vector2( P2[0] - P1[0], P2[1] - P1[1] )
def get_magnitude(self):
return math.sqrt( self.x**2 + self.y**2 )
def normalize(self):
magnitude = self.get_magnitude()
self.x /= magnitude
self.y /= magnitude
# rhs stands for Right Hand Side
def __add__(self, rhs):
return Vector2(self.x + rhs.x, self.y + rhs.y)
# vector subtraction method
def __sub__(self, rhs):
return Vector2(self.x - rhs.x, self.y - rhs.y)
# vetcor negation
def __neg__(self):
return Vector2(-self.x, -self.y)
# vector multiplication
def __mul__(self, scalar):
return Vector2(self.x * scalar, self.y * scalar)
# vector division
def __div__(self, scalar):
return Vector2(self.x / scalar, self.y / scalar)
# end Vector2
# ############################################################################
# class Avatar #
# 02/09/2008 #
# #
# class responsible for designing and move the sprite of avatar #
# ############################################################################
class Avatar(pygame.sprite.Sprite):
def __init__(self, startpos):
pygame.sprite.Sprite.__init__(self)
self.direction = 1
self.image, self.rect = load_image('a.png')
self.rect.centerx = startpos[0]
self.rect.centery = startpos[1]
# move the sprite
def move(self,x,y):
self.rect.move_ip(x,y)
# detection of edges - não esta funcionando
if self.rect.left < 0:
self.direction = 1
elif self.rect.right > width:
self.direction = -1
# end Avatar
# ############################################################################
# class Maps #
# 02/10/2008 #
# #
# class responsible for designing the map #
# ############################################################################
class Maps(pygame.sprite.Sprite):
def __init__(self, startpos):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('maps.bmp')
self.rect.centerx = startpos[0]
self.rect.centery = startpos[1]
# end Maps
# ############################################################################
# class Game #
# 02/09/2008 #
# #
# class main #
# ############################################################################
class Game():
avatar = Avatar([0,0])
maps = Maps([300,300])
av_pos = Vector2(320, 240)
clock = pygame.time.Clock()
av = avatar.image.subsurface((0, 0), (32, 32))
av_speed = 300.
y = 0
while 1:
# ensures that the program will not run more than 120fps
clock.tick(120)
key_direction = Vector2(1, 1)
# events of keyboard
for event in pygame.event.get():
if event.type == QUIT:
sys.exit(0)
# keyboard
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
key_direction.x = -1
av = avatar.image.subsurface((64, y), (32, 32))
y = y + 32
if y == 128:
y = 0
avatar.move(-1,0)
if event.key == pygame.K_RIGHT:
key_direction.x = +1
av = avatar.image.subsurface((32, y), (32, 32))
y = y + 32
if y == 128:
y = 0
avatar.move(1,0)
if event.key == pygame.K_UP:
key_direction.y = -1
av = avatar.image.subsurface((96, y), (32, 32))
y = y + 32
if y == 128:
y = 0
avatar.move(0,-1)
if event.key == pygame.K_DOWN:
key_direction.y = +1
av = avatar.image.subsurface((0, y), (32, 32))
y = y + 32
if y == 128:
y = 0
avatar.move(0,1)
if event.key == pygame.K_q:
sys.exit(0)
# mouse
elif event.type == MOUSEBUTTONDOWN:
# left
if event.button == 1:
x,y = pygame.mouse.get_pos()
#avatar.move(x,y)
# right
#if event.button == 3:
key_direction.normalize()
# detection between objects
#if avatar.rect.colliderect(maps.rect):
screen.fill(black)
#screen.blit(avatar.image, avatar.rect)
screen.blit(av, av_pos)
screen.blit(maps.image, maps.rect)
time_passed = clock.tick(30)
time_passed_seconds = time_passed / 3000.0
av_pos += key_direction * av_speed * time_passed_seconds
pygame.display.update()
pygame.display.flip()
# end Game
# ############################################################################
# def main #
# 02/09/2008 #
# #
# main function #
# ############################################################################
def main():
game = Game()
if __name__ == "__main__":
main()
# end main
Pergunta
Fabio Curtis
Boa tarde a todos, o que há de errado no meu código abaixo?? porque estou tentando fazer aparecer o sprite que têm as imagens do personagem andando para direita, esquerda, para cima e para baixo, mas dá erro:
Traceback (most recent call last):
File "/home/fabio/Projetos/Turismo3D/Source/Game.py", line 141, in
class Game():
File "/home/fabio/Projetos/Turismo3D/Source/Game.py", line 227, in Game
screen.blit(av, av_pos)
TypeError: invalid destination position for blit
Script terminated.
Segue abaixo o script
import pygame, sys, os, math from pygame.locals import * # initializing pygame pygame.init() # setting up the screen pygame.display.set_caption('Cenario') size = width, height = 800, 600 screen = pygame.display.set_mode((800, 600), RESIZABLE, 32) black = 0, 0, 0 window = pygame.display.set_mode((800, 600), RESIZABLE, 32) # fliping the display pygame.display.flip() # ############################################################################ # def load_image # # 02/09/2008 # # # # function that loads the images into memory # # ############################################################################ def load_image(name): fullname = os.path.join('imagens', name) try: image = pygame.image.load(fullname).convert_alpha() except pygame.error, message: print 'Cannot load image:', fullname raise SystemExit, message return image, image.get_rect() # end load_image # ############################################################################ # class Vector2 # # 02/16/2008 # # # # class responsible for the creation of vectors for moving images in 2D # # ############################################################################ class Vector2(object): def __init__(self, x=0.0, y=0.0): self.x = x self.y = y def __str__(self): return "(%s, %s)"%(self.x, self.y) @staticmethod def from_points(P1, P2): return Vector2( P2[0] - P1[0], P2[1] - P1[1] ) def get_magnitude(self): return math.sqrt( self.x**2 + self.y**2 ) def normalize(self): magnitude = self.get_magnitude() self.x /= magnitude self.y /= magnitude # rhs stands for Right Hand Side def __add__(self, rhs): return Vector2(self.x + rhs.x, self.y + rhs.y) # vector subtraction method def __sub__(self, rhs): return Vector2(self.x - rhs.x, self.y - rhs.y) # vetcor negation def __neg__(self): return Vector2(-self.x, -self.y) # vector multiplication def __mul__(self, scalar): return Vector2(self.x * scalar, self.y * scalar) # vector division def __div__(self, scalar): return Vector2(self.x / scalar, self.y / scalar) # end Vector2 # ############################################################################ # class Avatar # # 02/09/2008 # # # # class responsible for designing and move the sprite of avatar # # ############################################################################ class Avatar(pygame.sprite.Sprite): def __init__(self, startpos): pygame.sprite.Sprite.__init__(self) self.direction = 1 self.image, self.rect = load_image('a.png') self.rect.centerx = startpos[0] self.rect.centery = startpos[1] # move the sprite def move(self,x,y): self.rect.move_ip(x,y) # detection of edges - não esta funcionando if self.rect.left < 0: self.direction = 1 elif self.rect.right > width: self.direction = -1 # end Avatar # ############################################################################ # class Maps # # 02/10/2008 # # # # class responsible for designing the map # # ############################################################################ class Maps(pygame.sprite.Sprite): def __init__(self, startpos): pygame.sprite.Sprite.__init__(self) self.image, self.rect = load_image('maps.bmp') self.rect.centerx = startpos[0] self.rect.centery = startpos[1] # end Maps # ############################################################################ # class Game # # 02/09/2008 # # # # class main # # ############################################################################ class Game(): avatar = Avatar([0,0]) maps = Maps([300,300]) av_pos = Vector2(320, 240) clock = pygame.time.Clock() av = avatar.image.subsurface((0, 0), (32, 32)) av_speed = 300. y = 0 while 1: # ensures that the program will not run more than 120fps clock.tick(120) key_direction = Vector2(1, 1) # events of keyboard for event in pygame.event.get(): if event.type == QUIT: sys.exit(0) # keyboard elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: key_direction.x = -1 av = avatar.image.subsurface((64, y), (32, 32)) y = y + 32 if y == 128: y = 0 avatar.move(-1,0) if event.key == pygame.K_RIGHT: key_direction.x = +1 av = avatar.image.subsurface((32, y), (32, 32)) y = y + 32 if y == 128: y = 0 avatar.move(1,0) if event.key == pygame.K_UP: key_direction.y = -1 av = avatar.image.subsurface((96, y), (32, 32)) y = y + 32 if y == 128: y = 0 avatar.move(0,-1) if event.key == pygame.K_DOWN: key_direction.y = +1 av = avatar.image.subsurface((0, y), (32, 32)) y = y + 32 if y == 128: y = 0 avatar.move(0,1) if event.key == pygame.K_q: sys.exit(0) # mouse elif event.type == MOUSEBUTTONDOWN: # left if event.button == 1: x,y = pygame.mouse.get_pos() #avatar.move(x,y) # right #if event.button == 3: key_direction.normalize() # detection between objects #if avatar.rect.colliderect(maps.rect): screen.fill(black) #screen.blit(avatar.image, avatar.rect) screen.blit(av, av_pos) screen.blit(maps.image, maps.rect) time_passed = clock.tick(30) time_passed_seconds = time_passed / 3000.0 av_pos += key_direction * av_speed * time_passed_seconds pygame.display.update() pygame.display.flip() # end Game # ############################################################################ # def main # # 02/09/2008 # # # # main function # # ############################################################################ def main(): game = Game() if __name__ == "__main__": main() # end mainMuito Obrigado a todos
Link para o comentário
Compartilhar em outros sites
0 respostass a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.