Python点曲线

Aut*_*Mai 5 python pygame

我想用python创建一个游戏,但我需要一个发人深省的冲动,我如何画一个点,在他身后画一条线/一条小径,到目前为止,太好了,我不知道如何表达我的观点只是在 4 个方向上移动,我希望他自己向前移动,用户应该左右转向。


缺少的是:

• 我的点的轨迹(稍后我必须检查,如果另一个精灵接触它)
• “曲线”移动


我目前的代码:

import pygame
import os

pygame.init()
width, height = 970, 970
screen = pygame.display.set_mode((width, height))
h_center = ((height / 2) - 4)
w_center = ((width / 2) - 4)


class Point(pygame.sprite.Sprite):
    def __init__(self):
        self.image = pygame.image.load(os.path.join("assets", "point.png"))
        self.x = (width / 2)
        self.y = (height / 2)
        self.speed = 5
        self.direction = 3  # 1:north ; 2:east ; 3:south ; 4:west

    def handle_keys(self):
        key = pygame.key.get_pressed()
        dist = 1
        if key[pygame.K_DOWN]:
            self.direction = 3
        elif key[pygame.K_UP]:
            self.direction = 1
        if key[pygame.K_RIGHT]:
            self.direction = 2
        elif key[pygame.K_LEFT]:
            self.direction = 4

    def move(self):
        if self.direction == 1:
            self.y -= self.speed
        if self.direction == 2:
            self.x += self.speed
        if self.direction == 3:
            self.y += self.speed
        if self.direction == 4:
            self.x -= self.speed

    def draw(self, surface):
        surface.blit(self.image, (self.x, self.y))


def main():
    point = Point()
    clock = pygame.time.Clock()
    background = pygame.image.load('backgroundborder.png').convert()
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                running = False

        point.handle_keys()
        point.move()
        screen.fill((0, 0, 0))
        screen.blit(background, (0, 0))
        point.draw(screen)

        pygame.display.update()

        clock.tick(40)


if __name__ == '__main__':
    main()

Run Code Online (Sandbox Code Playgroud)

请协助