首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的对象不连续旋转?

为什么我的对象不连续旋转?
EN

Stack Overflow用户
提问于 2022-06-24 22:26:40
回答 1查看 40关注 0票数 1

错误:所以,我有一个逻辑错误。

我要我的物体“爪子”来回旋转(范围: 10-170度)。但当爪角达到170°时,夹角值没有减小。它坚持在170的角度。

,这是我的爪类

代码语言:javascript
复制
class Claw(pygame.sprite.Sprite):
    
    def __init__(self,image,position):
       
        super().__init__()
        self.original_image = image #saved original image(unchangeable)
        self.image = image
        self.rect = image.get_rect(center = position)

        self.offset = pygame.math.Vector2(default_offset_claw_x, 0)
        self.position = position

        self.direction = LEFT
        self.angle = 10 #the first angle
        self.angle_speed = 2.5
        

    def update(self):
       # rect_center = self.position + self.offset #mid point
       # self.rect = self.image.get_rect(center = rect_center)
        if self.direction == LEFT: #to the left
            self.angle += self.angle_speed
        elif self.direction == RIGHT:
            self.angle -= self.angle_speed
        

        #when it reaches an edge
        if self.angle > 170:
            self.angle = 170
            self.directon = RIGHT
        elif self.angle < 10:
            self.angle = 10
            self.direction = LEFT   

        print(self.angle, self.direction)  

**Here is my game main variables**

    default_offset_claw_x = 40
    LEFT = -1
    RIGHT = 1 

这里是我的主要

代码语言:javascript
复制
#claw initialize
claw_image = pygame.image.load(os.path.join(current_path, "img\\claw.png"))
claw = Claw(claw_image, (screen_width//2, 110))
#game loop
running = True

while running:

    clock.tick(30)  # FPS = 30
    # Look at every event in the queue
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    screen.blit(background,(0,0))
    
    gemstone_group.draw(screen) #draw all sprites in gemstone_group
    
    claw.update()
    claw.draw(screen)

    pygame.display.update() #display update

pygame.quit()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-25 07:54:42

i中缺少一个directon。一定是self.direction = RIGHT。无论如何,您可以简化代码:

代码语言:javascript
复制
class Claw(pygame.sprite.Sprite):
    # [...]

    def update(self):
        self.angle += self.angle_speed * self.direction
        if self.angle > 170 or self.angle < 10:
            self.anlge = max(10, min(170, self.angle))
            self.direction *= -1
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72749935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档