【发布时间】:2020-09-24 02:09:01
【问题描述】:
我决定用 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, fistx1, 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, y, fistx1, fisty1
fistx1 -= 25
window.fill(BACKGROUND_COLOR);
window.blit(self.fist1image, (fistx1, 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
【问题讨论】:
标签: python python-3.x pygame frame-rate