gpt4 book ai didi

python - 我似乎无法使用控件让我的 Sprite 旋转

转载 作者:行者123 更新时间:2023-12-01 02:22:31 26 4
gpt4 key购买 nike

class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
# currently this img stuff is a filler until I am bothered to do graphics or someone is willing to do them
self.image = player_img
self.image0 = new_player_img
self.rect = self.image.get_rect()
self.pos = vec(WIDTH / 2, HEIGHT / 2)
self.vel = vec(0, 0)
self.acc = vec(0, 0)
self.rot = 0
self.rot_speed = vec(14, -14)
self.last_update = pygame.time.get_ticks()

def rotate(self):
rkeys = pygame.key.get_pressed()
if rkeys[pygame.K_z]:
self.rot_speed.x = self.rot
if rkeys[pygame.K_c]:
self.rot_speed.y = self.rot

pygame.transform.rotate(self.image0, self.rot)
self.image = self.image0


def update(self):
self.acc = vec(0, 0)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.acc.x = -PLAYER_ACC
if keys[pygame.K_RIGHT]:
self.acc.x = PLAYER_ACC
if keys[pygame.K_UP]:
self.acc.y = -PLAYER_ACC
if keys[pygame.K_DOWN]:
self.acc.y = PLAYER_ACC

#apply translations
self.acc += self.vel * PLAYER_FRICTION
self.vel += self.acc
self.pos += self.vel + 0.5 * self.acc
self.image0 = self.image
self.rotate()

# wrapping around the screen for now until ive worked out a playing area
if self.pos.x > WIDTH:
self.pos.x = 0
if self.pos.x < 0:
self.pos.x = WIDTH
if self.pos.y > HEIGHT:
self.pos.y = 0
if self.pos.y < 0:
self.pos.y = HEIGHT

self.rect.center = self.pos

我不知道为什么,但由于某种原因,旋转功能什么也没做,我显然正在旋转并将玩家图像设置为旋转图像。谁能帮我这个?我需要它来让用户旋转 Sprite ,这样我就可以像汽车一样“驾驶” Sprite 。

最佳答案

pygame.transform.rotate 函数返回一个新的表面,并且不会修改原始图像,因此您必须将其分配给 self.image 属性。之后,您必须使用 self.image.get_rect 创建一个新的矩形,以便 Sprite 保持居中。

下一个问题是旋转速度。 rot 属性是当前角度,您必须通过每帧的 rot_speed 更新它,例如self.rot += self.rot_speed

我还将事件处理移至更新方法,并将所需的 rot_speed 传递给 rotate 方法。

update方法中,self.acc += self.vel * PLAYER_FRICTIONself.image0 = self.image导致错误行为,所以我删除了它们。

这是一个固定的最小和完整版本:

import pygame
from pygame.math import Vector2 as Vec

WIDTH, HEIGHT = 640, 480
PLAYER_FRICTION = .95
PLAYER_ACC = .1

class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((30, 50), pygame.SRCALPHA)
self.image.fill(pygame.Color('dodgerblue1'))
self.image0 = self.image
self.rect = self.image.get_rect()
self.pos = Vec(WIDTH / 2, HEIGHT / 2)
self.vel = Vec(0, 0)
self.acc = Vec(0, 0)
self.rot = 0
self.rot_speed = 3
self.last_update = pygame.time.get_ticks()

def rotate(self, rot_speed):
self.rot += rot_speed # Change `self.rot` (the angle).
# Rotate the original image.
self.image = pygame.transform.rotate(self.image0, self.rot)
# Get a new rect with the center of the old rect.
self.rect = self.image.get_rect(center=self.rect.center)

def update(self):
self.acc = Vec(0, 0)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.acc.x = -PLAYER_ACC
if keys[pygame.K_RIGHT]:
self.acc.x = PLAYER_ACC
if keys[pygame.K_UP]:
self.acc.y = -PLAYER_ACC
if keys[pygame.K_DOWN]:
self.acc.y = PLAYER_ACC
# Call self.rotate with the desired speed.
if keys[pygame.K_z]:
self.rotate(self.rot_speed)
if keys[pygame.K_c]:
self.rotate(-self.rot_speed)

# apply translations
self.vel += self.acc
self.vel *= PLAYER_FRICTION
self.pos += self.vel + 0.5 * self.acc

# wrapping around the screen
if self.pos.x > WIDTH:
self.pos.x = 0
if self.pos.x < 0:
self.pos.x = WIDTH
if self.pos.y > HEIGHT:
self.pos.y = 0
if self.pos.y < 0:
self.pos.y = HEIGHT

self.rect.center = self.pos


def main():
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
all_sprites = pygame.sprite.Group(Player())

done = False

while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True

all_sprites.update()
screen.fill((30, 30, 30))
all_sprites.draw(screen)
pygame.display.flip()
clock.tick(30)


if __name__ == '__main__':
pygame.init()
main()
pygame.quit()
<小时/>

如果你想像汽车一样移动物体,你还必须改变一些东西。我会将 self.acc 设置为所需的值,然后在按下向上或向下键时将其添加到更新方法中的 self.vel 中。然后,您还必须在 rotate 方法中旋转 self.accself.vel 向量,它就会按需要工作。

import pygame
from pygame.math import Vector2 as Vec

WIDTH, HEIGHT = 800, 600
PLAYER_FRICTION = .95

class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((30, 50), pygame.SRCALPHA)
self.image.fill(pygame.Color('dodgerblue1'))
self.image0 = self.image
self.rect = self.image.get_rect()
self.pos = Vec(WIDTH / 2, HEIGHT / 2)
self.vel = Vec(0, 0)
self.acc = Vec(0, -1)
self.rot = 0
self.rot_speed = 3
self.last_update = pygame.time.get_ticks()

def rotate(self, rot_speed):
self.rot += rot_speed # Change `self.rot` (the angle).
self.acc.rotate_ip(-rot_speed)
self.vel.rotate_ip(-rot_speed)
# Rotate the original image.
self.image = pygame.transform.rotate(self.image0, self.rot)
# Get a new rect with the center of the old rect.
self.rect = self.image.get_rect(center=self.rect.center)

def update(self):
keys = pygame.key.get_pressed()
# Call self.rotate with the desired speed.
if keys[pygame.K_LEFT]:
self.rotate(self.rot_speed)
if keys[pygame.K_RIGHT]:
self.rotate(-self.rot_speed)
# Accelerate if UP or DOWN get pressed.
if keys[pygame.K_UP]:
self.vel += self.acc
if keys[pygame.K_DOWN]:
self.vel += self.acc

pygame.display.set_caption('Vel {}, Acc: {}'.format(self.vel, self.acc))
# apply translations
self.vel *= PLAYER_FRICTION
self.pos += self.vel

# wrapping around the screen
if self.pos.x > WIDTH:
self.pos.x = 0
if self.pos.x < 0:
self.pos.x = WIDTH
if self.pos.y > HEIGHT:
self.pos.y = 0
if self.pos.y < 0:
self.pos.y = HEIGHT

self.rect.center = self.pos


def main():
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
all_sprites = pygame.sprite.Group(Player())

done = False

while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True

all_sprites.update()
screen.fill((30, 30, 30))
all_sprites.draw(screen)
pygame.display.flip()
clock.tick(30)


if __name__ == '__main__':
pygame.init()
main()
pygame.quit()

关于python - 我似乎无法使用控件让我的 Sprite 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47797253/

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