我决定改为在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来编写代码,并在命令提示符下运行它。我也有Windows10
最后,我尝试通过以下操作更改帧率:
以下是我已经研究过的问题:
clock.tick(360);
但是没有用
我已研究过这些问题:
https://gamedev.stackexchange.com/questions/48227/smooth-movement-pygame
我能发现其中的两个。首先,函数和类的定义应该在主循环之外,因为在一个循环中一遍又一遍地定义相同的东西没有任何意义。其次,您要调用
pygame.display.flip
两次,这是不需要的。翻转只能调用一次,否则会导致闪烁。第三,您正在使用__init__
方法进行绘制,并在每一帧创建一个新实例。通常,一个实例只生成一次,并且该实例的一些方法可以对该实例执行某些操作。因此,与其在__init__
中绘制,不如创建一个名为draw的新方法现在回答你的问题,它分块移动,因为:
pygame.MOUSEBUTTONDOWN
。此函数每次单击仅返回一次true。因此,如果按住鼠标按钮,它将不起作用,因为它在第一帧返回True
,然后返回None
。要持续更新鼠标状态,需要使用pygame.mouse.get_pressed()
李>实现了上面提到的所有内容的新代码(注意:我将图像更改为曲面以使其正常工作,所以您可能希望再次将其更改为图像):
Edit:忘记提到这个,但是您在
deal_damage
方法中使用了damage
参数,但没有使用它添加到@hippozipos答案中。使用
class
的一大好处是不需要使用global
变量。您只需将它们设置为属性您还试图在游戏循环中退出。 如果
running
是False
,它将退出游戏循环并自行退出也不需要在循环外填充,因为它每帧都被填充
我提供了一个如何实现这一目标的最简单的工作示例
相关问题 更多 >
编程相关推荐