gpt4 book ai didi

python - 在 pygame 中旋转图像,使其看起来像在旋转

转载 作者:太空宇宙 更新时间:2023-11-03 13:57:12 26 4
gpt4 key购买 nike

我正在创建一个游戏,玩家的攻击方式是旋转 360 度。我已经做到了,所以它确实做到了这一点,但是我遇到的问题是它每次旋转时都会回到原始图像。关于如何使图像不断旋转的任何想法

这是我的旋转代码片段:

self.rotate += 10
self.image_type = self.image
self.image = pygame.transform.rotate(self.image_type, self.rotate)

更新

这是我的大部分代码。我的程序分为两部分;主循环和类程序

我的类(class)代码:

import pygame
from pygame import *
import random
import time
import math as maths
pygame.init()

class character(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image1 = pygame.image.load("Knight_front.png").convert_alpha()
self.image_type = self.image1
self.image = self.image1
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = 300,215
self.st = 0
self.dir = 0
self.stab = False
self.rotate = 0

def update(self,keypress):
self.prevx,self.prevy = self.rect.x,self.rect.y
if keypress[K_w]:
self.image = self.image_type
self.rect.y -= 3
self.dir = 0
if keypress[K_s]:
self.image = pygame.transform.rotate(self.image_type, 180)
self.rect.y += 3
self.dir = 180
if keypress[K_a]:
self.image = pygame.transform.rotate(self.image_type, 90)
self.rect.x -= 3
self.dir = 90
if keypress[K_d]:
self.image = pygame.transform.rotate(self.image_type, -90)
self.rect.x += 3
self.dir = -90
if keypress[K_SPACE] and self.stab == False and time.time()>self.st+0.5:
self.rotate += 10
self.image_type = self.image
self.image = pygame.transform.rotate(self.image_type, self.rotate)
self.st = time.time()
self.stab = True
if time.time() > self.st + 0.25 and self.stab == True:
self.image_type = self.image1
self.image = pygame.transform.rotate(self.image_type, self.dir)
self.stab = False
if self.rotate == 360:
self.rotate = 0

我的主循环代码:

import pygame
from pygame import *

width, height = 640, 480
screen = pygame.display.set_mode((width,height))#,FULLSCREEN)

from classes import *
import random

spriteList = pygame.sprite.Group()
player = character()
spriteList.add(player)


Clock = pygame.time.Clock()

while True:

keypress = pygame.key.get_pressed()
screen.fill((255,255,255))
spriteList.draw(screen)
player.update(keypress)

pygame.event.pump()
Clock.tick(30)
pygame.display.update()

for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
quit()
if keypress[K_ESCAPE]:
pygame.quit()

最佳答案

如果按下空格键,我会将 self.stab 变量设置为 True,然后增加角度 (self.rotate) ,旋转图像并每帧获取一个新的矩形,当超过 360 度时,重置 self.stabself.rotate 变量。

if keypress[K_SPACE]:
self.stab = True

if self.stab:
self.rotate += 10
if self.rotate >= 360:
self.rotate = 0
self.stab = False
# Rotate the image and get a new rect with the old center.
self.image = pygame.transform.rotate(self.image_type, self.rotate)
self.rect = self.image.get_rect(center=self.rect.center)

关于python - 在 pygame 中旋转图像,使其看起来像在旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49511817/

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