在Pygame中难以让精灵移动

热爱气体

我试图使光标在图像上移动,但是在使光标图像(redcircle.png)移动时遇到问题。当我运行程序时,圆圈会像应有的那样跳到我的鼠标指针上,但是直到我重新加载它时才拒绝移动。我是python的新手,这些东西使我经历了一个循环,但是我在上面打开过的55个页面,教程和废话都没有给我一个明确的答案。

import pygame, random

pygame.init()

pygame.mouse.set_visible(True)

screen_width = 1250
screen_height = 800

size = screen_width, screen_height

speed = [60, 60]
black = (0, 0, 0)
WHITE = (255,255,255)
red = (255,0,0,125)

screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
FPS = 60

bg = pygame.image.load("redtest.png")
bgrect = bg.get_rect()
all_sprites_list = pygame.sprite.Group()

class Block(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        super().__init__()
        self.image = pygame.image.load("redcircle.png").convert_alpha()
        self.rect = self.image.get_rect()

block = Block(black, 20, 15)
all_sprites_list.add(block)

screen.fill(black)
screen.blit(bg, bgrect)
all_sprites_list.draw(screen)

def main():
    while True:
        pos = pygame.mouse.get_pos()
        block.rect.x = pos[0]
        block.rect.y = pos[1]
        clock.tick(FPS)
        pygame.display.update()
        pygame.display.flip()
main()
拉比德76

您必须在每帧中绘制背景并重新绘制Sprite组。此外,您必须处理事件。

主应用程序循环必须:

例如:

def main():
    run = True
    while run:
        clock.tick(FPS)

        # event loop
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        # update objects
        pos = pygame.mouse.get_pos()
        block.rect.x = pos[0]
        block.rect.y = pos[1]

        # draw background
        screen.blit(bg, bgrect)

        # d raw sprotes
        all_sprites_list.draw(screen)

        # update display
        pygame.display.update()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章