gpt4 book ai didi

Python点曲线

转载 作者:行者123 更新时间:2023-12-03 16:33:20 26 4
gpt4 key购买 nike

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

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

我目前的代码:

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

请协助

最佳答案

两件事情:

  • 使用列表来存储轨迹。这只是以前职位的列表。
  • 使用箭头键调整速度,而不是方向

  • 我还添加了几行代码,使点留在屏幕上。 dot.png 只是一个 20x20 像素的黑点。
    这是更新后的代码:
    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)

    trail=[None]*50 # trail has 50 dots
    trailimage = pygame.image.load('dot.png')

    class Point(pygame.sprite.Sprite):
    def __init__(self):
    self.image = pygame.image.load('dot.png')
    self.x = (width/2)
    self.y = (height/2)
    self.speed = {'x':0, 'y':0}
    self.direction = 3 # 1north ; 2east ; 3south ; 4west

    def handle_keys(self):
    key = pygame.key.get_pressed()
    dist = 1
    if key[pygame.K_DOWN]:
    self.speed['y']+=0.25
    elif key[pygame.K_UP]:
    self.speed['y']-=0.25
    if key[pygame.K_RIGHT]:
    self.speed['x']+=0.25
    elif key[pygame.K_LEFT]:
    self.speed['x']-=0.25

    def move(self):
    self.y += self.speed['y']
    self.x += self.speed['x']
    # wrap to other side of screen
    if self.x > width: self.x = (self.x - width)
    elif self.x < 0: self.x = (width + self.x)
    if self.y > height: self.y = (self.y - height)
    elif self.y < 0: self.y = (height + self.y)

    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((200, 200, 200))
    #screen.blit(background, (0, 0))
    for d in trail:
    if d: screen.blit(trailimage, d)
    del trail[0] # remove last point in trail
    trail.append((point.x,point.y)) # append this position

    point.draw(screen)

    pygame.display.update()

    clock.tick(40)

    if __name__ == '__main__':
    main()
    为了使控件更像 CurveFever,我更新了代码,以便向左\向右键调整行进方向(以度为单位)。速度是恒定的。
    这是更新后的代码:
    import pygame
    import os
    import math

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

    trail=[None]*50 # trail has 50 dots
    trailimage = pygame.image.load('dot.png')
    speed = 8 # constant

    class Point(pygame.sprite.Sprite):
    def __init__(self):
    self.image = pygame.image.load('dot.png')
    self.x = (width/2)
    self.y = (height/2)
    self.speed = {'x':0, 'y':0}
    self.deg = -90 # up, direction in degrees

    def handle_keys(self):
    key = pygame.key.get_pressed()
    dist = 1
    if key[pygame.K_RIGHT]:
    self.deg+=2
    elif key[pygame.K_LEFT]:
    self.deg-=2
    self.speed['x'] = speed*math.cos(math.radians(self.deg))
    self.speed['y'] = speed*math.sin(math.radians(self.deg))

    def move(self):
    self.y += self.speed['y']
    self.x += self.speed['x']
    # wrap to other side of screen
    if self.x > width: self.x = (self.x - width)
    elif self.x < 0: self.x = (width + self.x)
    if self.y > height: self.y = (self.y - height)
    elif self.y < 0: self.y = (height + self.y)

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


    TrailTrim = False # set True for constant trail length

    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((200, 200, 200)) # clear screen
    #screen.blit(background, (0, 0))
    for d in trail:
    if d: screen.blit(trailimage, d)
    if (TrailTrim): del trail[0] # delete trail end
    trail.append((point.x,point.y)) # add current postiion

    point.draw(screen) # draw current point

    pygame.display.update()

    clock.tick(40) # 40 FPS


    if __name__ == '__main__':
    main()

    关于Python点曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63133999/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com