Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
989 views
in Technique[技术] by (71.8m points)

python - Pygame game help: Easing/Acceleration

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...