Pygame精灵图形未更新

2024-09-28 20:52:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个pygame项目中遇到了一些困难,根据它的行进方向来更新精灵的外观。基本上,我想在正方形的下半部、上半部、左半部或右半部绘制一个矩形,如果它分别向上、向下、向右或向左移动。虽然我的debug print语句正确地显示了正方形的移动方向,但是pygame.draw.rect游戏()方法似乎对表面没有任何影响(自我形象)与跟踪对象关联。在

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

我不清楚我每帧都在更新跟踪对象的位置,但是图像仍然是黑色的,上面没有画任何矩形(我还没有让他们画到正方形的右边,我只想得到任何结果。)知道我在这里做错了什么吗?多谢了!在


Tags: rectimageselfifinitblueredpygame
1条回答
网友
1楼 · 发布于 2024-09-28 20:52:33

你必须用[0,0,7,15]self.imagedraw.rect(和draw.circle)。你不能使用rect.xrect.y,因为它可以给你[100,100,7,15],而且它在self.image区域之外。在

self.image区域上像素的坐标始终是x:0..14y:0..14

-

rect.xrect.ygroup.draw()在屏幕上的正确位置使用blit图像。在

相关问题 更多 >