Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&引用;旋转“;python中的硬币图像,pygame clicker游戏_Python_Python 3.x_Pygame_Pygame Surface - Fatal编程技术网

&引用;旋转“;python中的硬币图像,pygame clicker游戏

&引用;旋转“;python中的硬币图像,pygame clicker游戏,python,python-3.x,pygame,pygame-surface,Python,Python 3.x,Pygame,Pygame Surface,所以我现在正在做一个点击游戏,我已经有了一些好看的东西。但是因为我想改进游戏,我想在屏幕中央的硬币上添加一个旋转硬币动画 我有一个coin.py文件,它是这样的: import pygame class Coin(): def __init__(self, screen): self.screen = screen self.image = pygame.image.load('pyfiles/images/click_button.png')

所以我现在正在做一个点击游戏,我已经有了一些好看的东西。但是因为我想改进游戏,我想在屏幕中央的硬币上添加一个旋转硬币动画

我有一个
coin.py
文件,它是这样的:

import pygame

class Coin():
    def __init__(self, screen):
        self.screen = screen

        self.image = pygame.image.load('pyfiles/images/click_button.png')
        self.image_x = 230
        self.image_y = 130

        self.scale_change_x = 10
        self.scale_change_y = 10

    def blitme(self):
        self.screen.blit(self.image, (self.image_x, self.image_y))

当前的游戏玩法如下所示:

import pygame

class Coin():
    def __init__(self, screen):
        self.screen = screen

        self.image = pygame.image.load('pyfiles/images/click_button.png')
        self.image_x = 230
        self.image_y = 130

        self.scale_change_x = 10
        self.scale_change_y = 10

    def blitme(self):
        self.screen.blit(self.image, (self.image_x, self.image_y))

如你所见,当我的光标指向硬币图像时,它会变成黄色。但现在,我希望它不仅变成黄色,而且像这张图片一样旋转(我在谷歌上找到的):


我应该向我的
coin.py
文件中添加什么代码,使其在光标移动(与硬币碰撞)时执行此操作?

如果您有动画GIF,请参阅


如果您没有硬币的动画GIF或精灵片,您可以通过沿y轴挤压和翻转硬币来实现类似效果。
用于缩放图像和翻转图像。下面的代码片段创建了一个取决于特定角度的
硬币
表面的旋转效果:

new_width=round(数学sin(数学弧度(角度))*coin_rect.width)
rot_coin=新宽度大于等于0时的硬币pygame.transform.flip(硬币,真,假)
rot_coin=pygame.transform.scale(rot_coin,(abs(新宽度)、coin_rect.height))
blit(rot_coin,rot_coin.get_rect(中心=coin_rect.center))

最简单的例子:

导入pygame
输入数学
pygame.init()
window=pygame.display.set_模式((400400))
font=pygame.font.SysFont(无,40)
clock=pygame.time.clock()
coin=pygame.Surface((160160),pygame.SRCALPHA)
pygame.draw.circle(硬币,(255,255,0),(80,80),80,10)
pygame.draw.circle(硬币,(128,128,0),(80,80),75)
cointext=pygame.font.SysFont(无,80).render(“10”,True,(255,255,0))
coin.blit(cointext,cointext.get_rect(center=coin.get_rect().center))
coin_-rect=coin.get_-rect(中心=window.get_-rect().center)
角度=0
运行=真
运行时:
时钟滴答(60)
当前时间=pygame.time.get_ticks()
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
窗口填充(0)
新宽度=圆形(数学正弦(数学弧度(角度))*硬币矩形宽度)
角度+=2
rot_coin=新宽度大于等于0时的硬币pygame.transform.flip(硬币,真,假)
rot_coin=pygame.transform.scale(rot_coin,(abs(新宽度)、coin_rect.height))
blit(rot_coin,rot_coin.get_rect(中心=coin_rect.center))
pygame.display.flip()
pygame.quit()
退出()

您的硬币只是一个2D图像。你有硬币吗?(gif或精灵表)我尝试使用
.gif
扩展文件作为图像,但pygame显然无法打开这些文件。当我尝试时,我遇到了以下错误:
pygame.error:无法打开[image\u name.png]