Python Pygame精灵图形未更新
我在一个pygame项目中遇到了一些困难,它根据精灵的行进方向来更新精灵的外观。基本上,我想在一个正方形的下半部分、上半部分、左半部分或右半部分画一个矩形,如果它分别向上、向下、向右或向左移动。虽然我的debug print语句正确地显示了正方形的移动方向,但pygame.draw.rect()方法似乎并没有对与跟踪对象关联的曲面(self.image)做任何处理Python Pygame精灵图形未更新,python,pygame,surface,Python,Pygame,Surface,我在一个pygame项目中遇到了一些困难,它根据精灵的行进方向来更新精灵的外观。基本上,我想在一个正方形的下半部分、上半部分、左半部分或右半部分画一个矩形,如果它分别向上、向下、向右或向左移动。虽然我的debug print语句正确地显示了正方形的移动方向,但pygame.draw.rect()方法似乎并没有对与跟踪对象关联的曲面(self.image)做任何处理 import pygame black = ( 0, 0, 0) white = (255, 255, 255) blu
import pygame
black = ( 0, 0, 0)
white = (255, 255, 255)
blue = ( 0, 0, 255)
green = ( 0, 255, 0)
red = (255, 0, 0)
class Trace(pygame.sprite.Sprite):
def __init__(self,startx,starty):
super().__init__()
self.image = pygame.Surface([15,15])
self.rect = self.image.get_rect()
self.rect.x = startx
self.rect.y = starty
def update(self,walls):
self.image.fill(black)
pygame.draw.circle(self.image, red, [self.rect.x,self.rect.y], 7)
pos = pygame.mouse.get_pos()
diffx = pos[0] - self.rect.x
diffy = pos[1] - self.rect.y
#determine primary direction
if abs(diffx) >= abs(diffy):
if diffx >= 0:
print("moving right")
pygame.draw.rect(self.image, red, [self.rect.x,self.rect.y,7,15], 7)
else:
print("moving left")
pygame.draw.rect(self.image, red, [self.rect.x,self.rect.y,7,15], 7)
else:
if diffy >= 0 :
print("moving down")
pygame.draw.rect(self.image, red, [self.rect.x,self.rect.y,7,15], 7)
else:
print("moving up")
#move on x axis and check for collisions
self.rect.x += diffx/3
collisions = pygame.sprite.spritecollide(self, walls, False)
for wall in collisions:
if diffx > 0:
self.rect.right = wall.rect.left
else:
self.rect.left = wall.rect.right
#move on y axis and check for collisions
self.rect.y += diffy/3
collisions = pygame.sprite.spritecollide(self, walls, False)
for wall in collisions:
if diffy > 0:
self.rect.bottom = wall.rect.top
else:
self.rect.top = wall.rect.bottom
class Wall(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, color):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
pygame.init()
displaysize = [600,400]
screen = pygame.display.set_mode(displaysize)
clock = pygame.time.Clock()
done = False
testtrace = Trace(0,0)
tracelist = pygame.sprite.Group()
tracelist.add(testtrace)
wall = Wall(100,100,40,40,blue)
wall2 = Wall(155,100,40,40,blue)
wall3 = Wall(210,100,40,40,blue)
wall4 = Wall(100,155,40,40,blue)
wall5 = Wall(155,155,40,40,blue)
wall6 = Wall(210,155,40,40,blue)
walllist = pygame.sprite.Group()
walllist.add(wall,wall2,wall3,wall4,wall5,wall6)
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT: #close window on quit
done = True
screen.fill(white)
tracelist.update(walllist)
tracelist.draw(screen)
walllist.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
我不清楚我是否在每一帧更新跟踪对象的位置,但是图像仍然是黑色的,没有在任何地方绘制矩形(我还没有让它们绘制到正方形的右侧,我只是想在这一点上得到任何类型的结果。)知道我这里做错了什么吗?非常感谢 您必须使用
[0,0,7,15]
在self.image
上绘制.rect
(和绘制.circle
)。您不能使用rect.x
,rect.y
,因为它可以为您提供[100100,7,15]
,并且它位于self.image
区域之外
self.image
区域上像素的坐标始终为x:0..14
y:0..14
-
rect.x
rect.y
由group.draw()
用于将blit
图像显示在屏幕上的正确位置。您忘记了draw.rect
用于“向上移动”
是的,这样就可以了。这种疏忽正是拥有stackoverflow这样的资源非常有用的原因。谢谢你,伙计!