0

私は卓球ゲームを特定の fps でループを実行させようとしていますが、いくつか試してみましたがうまくいきませんでした。自分で出て

ループを特定の速度で実行して、よりスムーズに見えるようにしようとしています.dxまたは変更したときに移動する位置を編集すると、分厚いように見えるため、パドルの代わりに10 xダウン(見える分厚い)私はそれを1倍下に何度も動かしたいので、10倍と同じくらい速く下に移動しますが、よりスムーズです

完全なコード

import pygame #how to fix paddle widths from hitting ball
import sys
pygame.init()
screenSize = (800,600)
screen = pygame.display.set_mode((screenSize),0)
pygame.display.set_caption("HajarPongBasicCollision")

# colours
WHITE = (255,255,255)
BLACK = (0, 0, 0)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
PURPLE = (154, 136, 180)

screen.fill(BLACK)
pygame.display.update()
# retrieve screen measurements
screenw = screen.get_width()
screenh = screen.get_height()

# retrieve position of center of screen
centerx= 400 #tried not to use hard coded values but program gives me an error when i use screenw/2 ASK MR H TO HELP WITH NO HARD CODED VALUES (HCV)
centery= 300

# variables for first paddle
p1x = 10
p1y = 10
p1w = 10
p1h = 100

p1dy = 0
p1_score = 0

# variables for second paddle
p2w = 10
p2h = 100
p2x = screenw - 20
p2y = 10

p2dy = 0
p2_score = 0

# variable for ball
bx = 400 #HCV
by = 300 #HCV
br = 9
bdx = 1
bdy = 1

# speed of loop
fpsClock = pygame.time.Clock()
FPS = 60

go = True
while go:
    fpsClock.tick(FPS)
    for event in pygame.event.get():
        if event.type ==pygame.QUIT:
            go = False
        elif event.type == pygame.KEYDOWN:
            # control for the first paddle
            if event.key == pygame.K_w:
                    p1dy = -1
            elif event.key == pygame.K_s:
                    p1dy = 1
            # controls for the second paddle
            elif event.key == pygame.K_UP:
                    p2dy = -1
            elif event.key == pygame.K_DOWN:
                    p2dy = 1
            elif event.key == pygame.K_q:
                    go = False
        # stops rectangles from going continously
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w or event.key == pygame.K_s:
                p1dy = 0
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                p2dy = 0

    # stops paddle one from going off the screen
    if (p1y < 0):
        p1dy = 0
        p1y = 0
    if (p1y + p1h > screenh):
        p1dy = 0
        p1y = screenh - p1h

    # stops paddle two from going off the screen
    if (p2y < 0):
        p2dy = 0
        p2y = 0
    if (p2y + p2h > screenh):
        p2dy = 0
        p2y = screenh - p2h

    # stops ball from going off the screen
    if (bx + br >= screenw):
        bx = centerx
        by = centery
    elif (bx <= br):
        bx = centerx
        by = centery

    if (by + br >= screenh):
        bdy = -bdy
    elif (by <= br):
        bdy = -bdy

    # detects if ball hit paddles
    if bx - br <= p1x + p1w and by >= p1y and by <= p1y + p1h:
        bdx = -bdx

    if bx + br >= p2x and by >= p2y and by <= p2y + p2h:
        bdx = -bdx

    # moves the rectangles
    p1y = p1y + p1dy
    p2y = p2y + p2dy

    # moves the ball
    bx = bx + bdx
    by = by + bdy

    # removes screen trail
    screen.fill(BLACK)

    # draws the rectangles
    pygame.draw.rect(screen, WHITE,(p1x, p1y, p1w, p1h))
    pygame.draw.rect(screen, WHITE,(p2x, p2y, p2w, p2h))
    pygame.draw.circle(screen, WHITE, (bx, by), br, 0)
    pygame.display.update()

pygame.quit()
sys.exit()
4

1 に答える 1