如何解决在pygame中单击后如何平稳移动图像?
我决定改用pygame制作游戏,这是到目前为止我编写的代码:
from typing import Tuple
import pygame
from PIL import ImageGrab
# finding the height of the screen by taking a screenshot.
img = ImageGrab.grab()
(WIDTH,HEIGHT) = (img.size)
x = WIDTH / 2 - 470
y = HEIGHT / 2 - 400
fistx1 = x - 80
fistx2 = fistx1;
fisty1 = y + 40
fisty2 = y - 50
pygame.init()
clock = pygame.time.Clock()
RESOLUTION = (WIDTH,HEIGHT)
BACKGROUND_COLOR: Tuple[int,int,int] = (79,205,109)
MOVESPEED = 5
# window stuff
window = pygame.display.set_mode(RESOLUTION,flags=pygame.RESIZABLE,depth=32)
pygame.display.set_caption("The Connection")
window.fill(BACKGROUND_COLOR)
running = True
# all images for the game here
player_image = pygame.image.load("Connector.png")
player_fist1_image = pygame.image.load("Connector_hand.png")
player_fist2_image = player_fist1_image
while running:
pressed = pygame.key.get_pressed()
clicked = pygame.mouse.get_pressed();
class Player():
def __init__(self,hp,image,fist1image,fist2image):
global fisty1,fistx1,fisty2
self.hp = hp
self.image = image
self.fist1image = fist1image
self.fist2image = fist2image
window.blit(self.image,(x,y))
window.blit(self.fist1image,(fistx1,fisty1))
window.blit(self.fist2image,(fistx2,fisty2))
def move(self,movespeed):
global x,y,fisty1,fisty2
if pressed[pygame.K_a]:
x -= movespeed
fistx1 -= movespeed
elif pressed[pygame.K_d]:
x += movespeed
fistx1 += movespeed
elif pressed[pygame.K_w]:
y -= movespeed
fisty1 -= movespeed
fisty2 -= movespeed
elif pressed[pygame.K_s]:
y += movespeed
fisty1 += movespeed
fisty2 += movespeed
def deal_damage(self,damage):
global x,fisty1
fistx1 -= 25
window.fill(BACKGROUND_COLOR);
window.blit(self.fist1image,fisty1));
pygame.display.flip();
# this is the function we use to actually call it. This is more helpful and less confusing for an idiot like me
def actual_deal():
main_player.deal_damage(25);
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
try:
pygame.quit()
except pygame.error:
pass
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
actual_deal();
window.fill(BACKGROUND_COLOR)
main_player = Player(100,player_image,fist1image=player_fist1_image,fist2image=player_fist2_image)
main_player.move(movespeed=MOVESPEED)
pygame.display.flip()
clock.tick(60);
对于上下文,拳头必须前进然后后退。但是,拳头动作不平稳,有两个问题:
-
看起来不太好。我想把这个展示给其他人,所以我希望它看起来不错。
-
当我缩回拳头时,看起来好像它们并没有向前移动。我必须在两者之间添加
time.sleep
,但我不愿意这样做。
这是我打孔时得到的输出: 放置在扰流板中,以免干扰
如您所见,它以块状方式移动。如果要查看我想要的输出,请使用WASD键移动字符并查看字符移动的平滑程度。我想要拳头也一样。
如果有关系,我正在使用pycharm对此进行编码,并从命令提示符处运行它。我也有Windows 10。
最后,我尝试通过执行以下操作更改帧速率:
以下是我已经看过的问题:
clock.tick(360);
但无济于事。
我已经看过以下问题:
https://gamedev.stackexchange.com/questions/48227/smooth-movement-pygame
解决方法
我可以发现这对夫妇。首先,函数和类的定义应该在主循环之外,因为在循环中一遍又一遍地定义同一件事没有任何意义。其次,您两次呼叫pygame.display.flip
,这不是必需的。翻转仅应调用一次,否则会导致闪烁。第三,您正在绘制__init__
方法并在每帧中创建一个新实例。通常,一个实例仅创建一次,并且该实例有一些方法可以对该实例执行某些操作。因此,不要绘制__init__
,而是创建一个名为draw的新方法。
现在要回答您的问题,由于以下原因,它成块地移动:
- 您一次将其移动25帧,因此它会一次跳过25个像素并在新位置再次绘制。
- 您正在使用
pygame.MOUSEBUTTONDOWN
。每次点击一次,此函数仅返回一次true。因此,如果您按住鼠标按钮,它将不起作用,因为它在第一帧中返回True
,在此之后返回None
。要连续更新鼠标状态,您需要使用pygame.mouse.get_pressed()
。
上面提到的所有代码都实现了新代码(注意:我将图像更改为表面以使其起作用,所以您可能想再次将其更改为图像):
from typing import Tuple
import pygame
from PIL import ImageGrab
# finding the height of the screen by taking a screenshot.
img = ImageGrab.grab()
(WIDTH,HEIGHT) = (img.size)
x = WIDTH / 2 - 470
y = HEIGHT / 2 - 400
fistx1 = x - 80
fistx2 = fistx1;
fisty1 = y + 40
fisty2 = y - 50
pygame.init()
clock = pygame.time.Clock()
RESOLUTION = (WIDTH,HEIGHT)
BACKGROUND_COLOR: Tuple[int,int,int] = (79,205,109)
MOVESPEED = 5
# window stuff
window = pygame.display.set_mode(RESOLUTION,flags=pygame.RESIZABLE,depth=32)
pygame.display.set_caption("The Connection")
window.fill(BACKGROUND_COLOR)
running = True
# all images for the game here
player_image = pygame.Surface((50,50)).convert()
player_fist1_image = pygame.Surface((10,10)).convert()
player_fist2_image = player_fist1_image
class Player():
def __init__(self,hp,image,fist1image,fist2image):
global fisty1,fistx1,fisty2
self.hp = hp
self.image = image
self.fist1image = fist1image
self.fist2image = fist2image
def move(self,movespeed):
global x,y,fisty1,fisty2
if pressed[pygame.K_a]:
x -= movespeed
fistx1 -= movespeed
elif pressed[pygame.K_d]:
x += movespeed
fistx1 += movespeed
elif pressed[pygame.K_w]:
y -= movespeed
fisty1 -= movespeed
fisty2 -= movespeed
elif pressed[pygame.K_s]:
y += movespeed
fisty1 += movespeed
fisty2 += movespeed
def draw(self):
window.blit(self.image,(x,y))
window.blit(self.fist1image,(fistx1,fisty1))
window.blit(self.fist2image,(fistx2,fisty2))
def deal_damage(self,damage):
global x,fisty1
mousePressed = pygame.mouse.get_pressed()
if mousePressed[0]:
fistx1 -= 1
window.fill(BACKGROUND_COLOR);
window.blit(self.fist1image,fisty1));
#pygame.display.flip();
main_player = Player(100,player_image,fist1image=player_fist1_image,fist2image=player_fist2_image)
# this is the function we use to actually call it. This is more helpful and less confusing for an idiot like me
def actual_deal():
main_player.deal_damage(25);
while running:
pressed = pygame.key.get_pressed()
clicked = pygame.mouse.get_pressed();
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
try:
pygame.quit()
except pygame.error:
pass
window.fill(BACKGROUND_COLOR)
main_player.move(movespeed=MOVESPEED)
main_player.deal_damage(50)
main_player.draw()
pygame.display.flip()
clock.tick(60);
编辑:忘记提及这一点,但是您正在使用damage
方法中的deal_damage
自变量,而不使用它。
要添加到@hippozhipos答案中。使用Number
的一大好处是您不需要使用Any
变量。您只需将它们设置为属性。
您还试图在游戏循环中退出。
如果class
为global
,它将退出游戏循环并自行退出。
由于循环每帧都被填充,因此也无需填充循环。
我提供了一个最小的工作示例,说明如何实现该目标。
running
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。