Python 3.x 在类外使用Def短期影响类
我希望能够在屏幕上旋转一个类,但仅当它与另一个类碰撞时。因此,我可以在类之外单独定义旋转,以便在类返回到其自身的内部定义函数之前,它只在特定的时间段内生效吗Python 3.x 在类外使用Def短期影响类,python-3.x,pygame,Python 3.x,Pygame,我希望能够在屏幕上旋转一个类,但仅当它与另一个类碰撞时。因此,我可以在类之外单独定义旋转,以便在类返回到其自身的内部定义函数之前,它只在特定的时间段内生效吗 基本上,我想要一辆赛车在碰到油膜时旋转。所有这些都可以在物体内完成。我将采取的方法是存储碰撞发生的时间,在进一步设置的时间内运行碰撞动画,然后更改回正常视图。PyGame有一个可通过该函数访问的实时毫秒时钟 这对你每天的雪碧课来说并不是一个巨大的增加。首先,给定一些动画参数,使用PyGame制作一组图像的旋转副本。然后,当触发碰撞/旋转时,
基本上,我想要一辆赛车在碰到油膜时旋转。所有这些都可以在物体内完成。我将采取的方法是存储碰撞发生的时间,在进一步设置的时间内运行碰撞动画,然后更改回正常视图。PyGame有一个可通过该函数访问的实时毫秒时钟 这对你每天的雪碧课来说并不是一个巨大的增加。首先,给定一些动画参数,使用PyGame制作一组图像的旋转副本。然后,当触发碰撞/旋转时,使用经过的毫秒时间(自事件开始以来)单步执行动画
import pygame
import random
# Window size
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
WINDOW_SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
GRASS_GREEN = ( 3, 255, 54)
class CarSprite( pygame.sprite.Sprite ):
SPIN_TIME = 1950 # milliseconds spent spinning
ANIM_DELAY = 50 # milliseconds per frame of animation
def __init__( self, car_image, x, y ):
pygame.sprite.Sprite.__init__(self)
self.image = car_image
self.rect = self.image.get_rect()
self.rect.center = ( x, y )
self.rotations = [ self.image ]
self.spinning_since = 0 # not spinning (yet)
self.makeRotations() # create animation frames
def makeRotations( self ):
""" Pre-generate the rotated images for animating the spin """
ROT_FRAMES = 36 # frames of rotation
for i in range( ROT_FRAMES ):
angle = i * ( 360 / ROT_FRAMES )
rotated_image = pygame.transform.rotozoom(self.image, angle, 1)
self.rotations.append( rotated_image )
def spin( self ):
""" Trigger a spinning action """
if ( self.spinning_since == 0 ):
self.spinning_since = pygame.time.get_ticks() # Start spinning
else:
pass # we're spinning already
def update( self ):
# If the car is spinning, set the frame of animation
if ( self.spinning_since > 0 ):
time_spinning = pygame.time.get_ticks() - self.spinning_since
if ( time_spinning >= CarSprite.SPIN_TIME ):
# Stop spinning, we've spun enough
self.spinning_since = 0
index = 0
else:
# Map the time to the appropriate animation
index = int( time_spinning / CarSprite.ANIM_DELAY )
index %= len( self.rotations ) # ensure it's a valid index
# update the image, preserving the location about the centroid.
cx, cy = self.rect.center # preserve the centre of the image
self.image = self.rotations[ index ] # use the new image
self.rect = self.image.get_rect()
self.rect.centerx = cx # restore the old centre (bitmaps will be different sized)
self.rect.centery = cy
### MAIN
pygame.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), WINDOW_SURFACE )
pygame.display.set_caption("Collision Spin")
# Add some cars
car_image = pygame.image.load('car.png')
sprites = pygame.sprite.Group()
for i in range(3):
# Random position within the middle 80% of screen
pos_x = random.randrange( WINDOW_WIDTH//10, WINDOW_WIDTH-WINDOW_WIDTH//10 )
pos_y = random.randrange( WINDOW_HEIGHT//10, WINDOW_HEIGHT-WINDOW_HEIGHT//10 )
sprites.add( CarSprite( car_image, pos_x, pos_y ) )
clock = pygame.time.Clock()
done = False
while not done:
# re-position all the cars
sprites.update()
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
elif ( event.type == pygame.MOUSEBUTTONDOWN ):
# On mouse-click, tell *all* the cars they've collided
for car in sprites:
car.spin()
# Movement keys
keys = pygame.key.get_pressed()
#if ( keys[pygame.K_UP] ):
# print("up")
# Update the window, but not more than 60fps
window.fill( GRASS_GREEN )
sprites.draw( window )
pygame.display.flip()
# Clamp FPS
clock.tick_busy_loop(60)
pygame.quit()
从Vectezy拍摄的矢量汽车图像,根据许可证使用。(已转换为位图。)
car.png
所有这些都可以在对象内完成。我将采取的方法是存储碰撞发生的时间,在进一步设置的时间内运行碰撞动画,然后更改回正常视图。PyGame有一个可通过该函数访问的实时毫秒时钟
这对你每天的雪碧课来说并不是一个巨大的增加。首先,给定一些动画参数,使用PyGame制作一组图像的旋转副本。然后,当触发碰撞/旋转时,使用经过的毫秒时间(自事件开始以来)单步执行动画
import pygame
import random
# Window size
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
WINDOW_SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
GRASS_GREEN = ( 3, 255, 54)
class CarSprite( pygame.sprite.Sprite ):
SPIN_TIME = 1950 # milliseconds spent spinning
ANIM_DELAY = 50 # milliseconds per frame of animation
def __init__( self, car_image, x, y ):
pygame.sprite.Sprite.__init__(self)
self.image = car_image
self.rect = self.image.get_rect()
self.rect.center = ( x, y )
self.rotations = [ self.image ]
self.spinning_since = 0 # not spinning (yet)
self.makeRotations() # create animation frames
def makeRotations( self ):
""" Pre-generate the rotated images for animating the spin """
ROT_FRAMES = 36 # frames of rotation
for i in range( ROT_FRAMES ):
angle = i * ( 360 / ROT_FRAMES )
rotated_image = pygame.transform.rotozoom(self.image, angle, 1)
self.rotations.append( rotated_image )
def spin( self ):
""" Trigger a spinning action """
if ( self.spinning_since == 0 ):
self.spinning_since = pygame.time.get_ticks() # Start spinning
else:
pass # we're spinning already
def update( self ):
# If the car is spinning, set the frame of animation
if ( self.spinning_since > 0 ):
time_spinning = pygame.time.get_ticks() - self.spinning_since
if ( time_spinning >= CarSprite.SPIN_TIME ):
# Stop spinning, we've spun enough
self.spinning_since = 0
index = 0
else:
# Map the time to the appropriate animation
index = int( time_spinning / CarSprite.ANIM_DELAY )
index %= len( self.rotations ) # ensure it's a valid index
# update the image, preserving the location about the centroid.
cx, cy = self.rect.center # preserve the centre of the image
self.image = self.rotations[ index ] # use the new image
self.rect = self.image.get_rect()
self.rect.centerx = cx # restore the old centre (bitmaps will be different sized)
self.rect.centery = cy
### MAIN
pygame.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), WINDOW_SURFACE )
pygame.display.set_caption("Collision Spin")
# Add some cars
car_image = pygame.image.load('car.png')
sprites = pygame.sprite.Group()
for i in range(3):
# Random position within the middle 80% of screen
pos_x = random.randrange( WINDOW_WIDTH//10, WINDOW_WIDTH-WINDOW_WIDTH//10 )
pos_y = random.randrange( WINDOW_HEIGHT//10, WINDOW_HEIGHT-WINDOW_HEIGHT//10 )
sprites.add( CarSprite( car_image, pos_x, pos_y ) )
clock = pygame.time.Clock()
done = False
while not done:
# re-position all the cars
sprites.update()
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
elif ( event.type == pygame.MOUSEBUTTONDOWN ):
# On mouse-click, tell *all* the cars they've collided
for car in sprites:
car.spin()
# Movement keys
keys = pygame.key.get_pressed()
#if ( keys[pygame.K_UP] ):
# print("up")
# Update the window, but not more than 60fps
window.fill( GRASS_GREEN )
sprites.draw( window )
pygame.display.flip()
# Clamp FPS
clock.tick_busy_loop(60)
pygame.quit()
从Vectezy拍摄的矢量汽车图像,根据许可证使用。(已转换为位图。)
car.png
我想您需要为上下文显示一些代码。在类中拥有一个方法并不意味着你不能控制它何时执行。您需要调用met)函数才能运行它们;不管他们在哪里。我认为你所说的可以通过多态性来处理。看看这一点和其他OOP原则。我认为您需要为上下文显示一些代码。在类中拥有一个方法并不意味着你不能控制它何时执行。您需要调用met)函数才能运行它们;不管他们在哪里。我认为你所说的可以通过多态性来处理。看看这个和其他OOP原则。哦,这应该存储以前的图像索引,然后只有在发生更改时才重新设置图像和rect。我想这是留给读者的一个优化!如果pygame.sprite.spritecollide()发生在金斯利,我想我可能已经通过调用car.spin()找到了答案。。。。非常感谢-我让它工作得很好,完全符合我的要求:)哦,这应该存储以前的图像索引,然后只有在发生更改时才重新设置图像和rect。我想这是留给读者的一个优化!如果pygame.sprite.spritecollide()发生在金斯利,我想我可能已经通过调用car.spin()找到了答案。。。。非常感谢-我让它工作得很好,而且做的正是我想要的:)