简体   繁体   English

pygame如何让屏幕抖动?

[英]How to make the screen shake in pygame?

I am making a game, and when I fire a bullet I want to make the screen shake.我正在制作游戏,当我发射子弹时,我想让屏幕抖动。 I can't find anything on Google so I thought I'd ask here.我在谷歌上找不到任何东西,所以我想我会在这里问。 Just maybe some examples, thanks.只是一些例子,谢谢。 I don't have any code yet, so I wont display it here.我还没有任何代码,所以我不会在这里展示它。

I'm thinking something like this:我在想这样的事情:

if space bar clicked:
     screenshake()

First let's think about a cool shaking function through time, like a damped sine wave .首先让我们想一想 function 经过时间的酷摇,就像一个阻尼正弦波

It has the mathematical form:它具有数学形式:

f(t) = A*e^(-k*t)*sin(w*t + p)

where A is the amplitude, -k the decaying factor, w the angular frequency and p the phase angle.In Pygame, your whole screen is a surface.其中 A 是幅度,-k 是衰减因子,w 是 angular 频率,p 是相位角。在 Pygame 中,你的整个屏幕是一个表面。 Assuming that you called your main surface screen , and that you sampled a few (x, y) points from f(t) (you can use different A, w, and p values for x and y), you can shake your screen as follow when you update the frame:假设您调用了主表面screen ,并且您从 f(t) 中采样了几个 (x, y) 点(您可以为 x 和 y 使用不同的 A、w 和 p 值),您可以将屏幕摇动为更新框架时遵循:

tmp_screen = screen.copy
screen.fill((0,0,0))  # erase current screen
screen.blit(tmp_screen,(x_shift,y_shift)) # reprint previous screen but shifted

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM