Pygame 游戏帮助:缓动/加速 [英] Pygame game help: Easing/Acceleration

查看:43
本文介绍了Pygame 游戏帮助:缓动/加速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我玩我的 pygame 游戏,这是我的第一款游戏,我对此非常不擅长.本质上,我试图制作一种相扑游戏,其中 2 名玩家在冰环(圆形舞台)上,他们必须互相推开才能得分,我现在在冰上物理方面遇到问题,我知道必须有按住键时的某种类型的加速和释放时的摩擦力,我现在正在尝试这样做,但目前当按下键时,它只会增加一次速度,而不是持续增加速度,这意味着您必须点击它才能继续快点.另外,如果你想帮助我玩游戏,如果我以后有任何问题,我将不胜感激,如果你想补充,我有不和谐之处:vincent#3996

导入pygame、sys、time从 pygame.locals 导入 *随机导入#颜色colorRed=pygame.Color(241,59,62)colorPurple=pygame.Color(200,254,249)colorBlue=pygame.Color(52, 207, 235)colorGreen=pygame.Color(100,182,100)colorWhite=pygame.Color(255,250,250)colorBlack=pygame.Color(0,0,0)colorOrange=pygame.Color(242,164,0)colorBrown=pygame.Color(148,103,58)#方面w=800高=600pygame.init()fpsClock=pygame.time.Clock()屏幕=pygame.display.set_mode((w,h))pygame.display.set_caption ('相扑')中心X=w//2中心Y=h//2#阶段阶段R=250定义阶段(centerX,centerY):"""stage (centerX,centerY) - 创建一个给定中心点的舞台"""pygame.draw.circle(屏幕,colorBlue,(centerX,centerY),stageR)#角色1xR=int((stageR//10))x1=int(centerX-(stageR*0.8))y1=centerYx1_dir=0y1_dir=0x1_right=假def char1 (x1,y1):"""char1 (x1,y1) - 在给定坐标处创建 char1"""pygame.draw.circle(屏幕,颜色红色,(x1,y1),xR)打印 (x1)打印(centerX)如果 x1_right==True:x1_dir+2为真:屏幕填充(颜色黑色)对于 pygame.event.get() 中的事件:#游戏退出如果 event.type== 退出:pygame.quit()系统退出()如果 event.type==KEYDOWN:如果 event.key==K_d:x1_dir+=1x1_right=真如果 event.key==K_a:x1_dir-=1如果 event.key==K_w:y1_dir-=1如果 event.key==K_s:y1_dir+=1如果 event.type==KEYUP:如果 event.key==K_d:x1_right=假舞台(centerX,centerY)字符 1 (x1,y1)x1+=x1_diry1+=y1_dirpygame.display.update()fpsClock.tick(60)

解决方案

KEYDOWN 事件只发生一次,当按键被按下时(参见

Hi can someone help me with my pygame game, it's my first game and im really bad at this. Essentially im trying to make one of those sumo games where 2 players are on an icey ring (circle stage) and they have to push each other off to score points, im having trouble with the ice physics right now i understand that there has to be some type of acceleration when the key is held down and friction when it is released and im trying to do that right now but currently when the key is pressed it only increases the speed once, not continually which means u have to spam click it to go faster. Also if you would like to help me with my game if i have any questions later i would greatly appreciate it uh i have discord if you would like to add thanks : vincent#3996

import pygame, sys, time
from pygame.locals import *
import random

#Colors
colorRed=pygame.Color(241,59,62)
colorPurple=pygame.Color(200,254,249)
colorBlue=pygame.Color(52, 207, 235)
colorGreen=pygame.Color(100,182,100)
colorWhite=pygame.Color(255,250,250)
colorBlack=pygame.Color(0,0,0)
colorOrange=pygame.Color(242,164,0)
colorBrown=pygame.Color(148,103,58)

#Dimensions
w=800
h=600
pygame.init()
fpsClock=pygame.time.Clock()
screen=pygame.display.set_mode((w,h))
pygame.display.set_caption ('SUMO')
centerX=w//2
centerY=h//2

#Stage
stageR=250
def stage (centerX,centerY):
    """stage (centerX,centerY) - creates a stage with given centerpoint"""
    pygame.draw.circle(screen, colorBlue, (centerX,centerY),stageR)

#Character 1
xR=int((stageR//10))
x1=int(centerX-(stageR*0.8))
y1=centerY
x1_dir=0
y1_dir=0
x1_right=False
def char1 (x1,y1):
    """char1 (x1,y1) - creates char1 at given coordinates"""
    pygame.draw.circle(screen, colorRed, (x1,y1),xR)
print (x1)
print (centerX)
if x1_right==True:
    x1_dir+2

while True:
    screen.fill(colorBlack)
    for event in pygame.event.get():
        #Game Exit
        if event.type== QUIT:
            pygame.quit()
            sys.exit()
        if event.type==KEYDOWN:
            if event.key==K_d:
                x1_dir+=1
                x1_right=True
            if event.key==K_a:
                x1_dir-=1
            if event.key==K_w:
                y1_dir-=1
            if event.key==K_s:
                y1_dir+=1
        if event.type==KEYUP:
            if event.key==K_d:
                x1_right=False

    stage (centerX,centerY)
    char1 (x1,y1)
    x1+=x1_dir
    y1+=y1_dir
    pygame.display.update()
    fpsClock.tick(60)

解决方案

The KEYDOWN event occurse only once, when the key is pressed (see pygame.event).
Use pygame.key.get_pressed() to get the current state of the keys in each frame.
Increase the speed in x direction (x1_dir) if a or d is pressed and decrease it if no one of the keys is pressed.
Increase the speed in y direction (y1_dir) if w or s is pressed and decrease it if no one of the keys is pressed. e.g.:

while True:
    screen.fill(colorBlack)
    for event in pygame.event.get():
        #Game Exit
        if event.type== QUIT:
            pygame.quit()
            sys.exit()
        if event.type==KEYDOWN:
            if event.key==K_d:
                x1_right=True
        if event.type==KEYUP:
            if event.key==K_d:
                x1_right=False

    keys = pygame.key.get_pressed()

    if keys[K_d] or keys[K_a]:
        x1_dir += 0.1 if keys[K_d] else -0.1
    else:
        x1_dir *= 0.98

    if keys[K_w] or keys[K_s]:
        y1_dir += 0.1 if keys[K_s] else -0.1
    else:
        y1_dir *= 0.98

For a smooth movement you have to use floating point values. Use round to convert floating point numbers to integral integral numbers e.g.

char1(round(x1), round(y1))

这篇关于Pygame 游戏帮助:缓动/加速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆