Мои лопатки движутся противоположными клавишами, левая лопатка движется со стрелками, а правая лопатка движется вместе с wsad. Как я могу это исправить? - PullRequest
0 голосов
/ 19 марта 2020

Я повторяю игру c понг, и мои весла противоположны. Мое левое весло движется со стрелками, а правое - с wsad, поэтому, когда вы играете, игроки играют, они должны перемещать противоположное весло, на котором они находятся. Кроме того, я пытаюсь заставить мяч вернуться, когда он находится за пределами экрана, и я играл с ним, и теперь происходит столкновение со стеной и веслом, что делает его бесконечной игрой вместо закругленного понга.

# import the necessary modules
import pygame
import sys
import time

#initialize pygame
pygame.init()
screenSize=(700,500)
screen=pygame.display.set_mode((screenSize), 0)

# set the caption for the screen
pygame.display.set_caption("Meryem's Pong Game")

# define colours you will be using
WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
YELLOW = (255,255,0)

# set the size for the surface (screen)
screen_h = screen.get_height()
screen_w = screen.get_width()
cx = int(screen_w/2)
cy = int(screen_h/2)

#initialize variables for player
#variables for first rectangle
R1x = 660
R1y = 300
R1w = 10
R1h = 132

R1dx = 0
R1dy = 0
R1_score = 0

#variables for second rectangle
R2x = 10
R2y = 2
R2w = 10
R2h = 132

R2dx = 0
R2dy = 0
R2_score = 0

#ball variables
bx = cx
by = cy
dby = 3
dbx = 3
br = 5
cy =  screen.get_height()/2
cx =  screen.get_width()/2

# variable for scores
R1_score = 0
R2_score = 0

playerRect = pygame.Rect(R2x,R2y,R2w, R2h)
playerRect2 = pygame.Rect(R1x,R1y,R1w, R1h)
ballRect = pygame.Rect (cx,cy,30,30)

#speed
speed = 3

fontsize = 50
fontScore = pygame.font.SysFont('arial', 50)
fontScore = pygame.font.SysFont('arial', 50)

R1Score = fontScore.render(str(R1_score), True, (WHITE))
R2Score = fontScore.render(str(R2_score), True, (WHITE))

# speed of object "clock"
clock = pygame.time.Clock()

FPS_easy = 30 # set frames per second for easy level
FPS_medium = 80 # set frames per second for medium level
FPS_progressive = 100 # set frames per second for progressive level

# set main loop to True so it will run
main = True
# main loop
while main:
    for event in pygame.event.get(): # check for any events (i.e key press, mouse click etc.)
        if event.type ==pygame.QUIT: # check to see if it was "x" at top right of screen
            main = False         # set the "main" variable to False to exit while loop
        if event.type ==pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                R1dx = 0
                R1dy = -speed
            elif event.key == pygame.K_DOWN:
                R1dx = 0
                R1dy = speed
            if event.key == pygame.K_w:
                R2dx = 0
                R2dy = -speed
            elif event.key == pygame.K_s:
                R2dx = 0
                R2dy = speed
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                R1dx = 0
                R1dy = 0
            elif event.key == pygame.K_w or event.key == pygame.K_s:
                R2dx = 0
                R2dy = 0

    playerRect.y = playerRect.y + R1dy
    playerRect2.y = playerRect2.y + R2dy
    ballRect.move_ip(dbx,dby)


    #collision of ball
    if ballRect.top <= 0:
        dby = -dby
    if ballRect.bottom >= screen_h:
        dby = -dby

    if ballRect.left <= 0:
        dbx = -dbx
    if ballRect.right >= screen_w:
        dbx = -dbx

    if ballRect.colliderect(playerRect2):
        dbx = -dbx
    if ballRect.colliderect(playerRect):
        dbx = -dbx

    screen.fill(BLACK)

    # draw the shapes, in this case the blue rectangles
    pygame.draw.rect(screen, WHITE,(playerRect),0)
    pygame.draw.rect(screen, WHITE,(playerRect2),0)
    pygame.draw.circle(screen, RED,ballRect.center,br,0)
    screen.blit(R1Score, (280,10))
    screen.blit(R2Score, (400,10))

    # we are using .flip() here,  it basically works the same as .update()
    # we will discuss this more in class (you can use either one)
    pygame.display.flip()

# quit pygame and exit the program (i.e. close everything down)
pygame.quit()
sys.exit()

Ответы [ 2 ]

0 голосов
/ 19 марта 2020

Вы перепутали R1x, R1y, R1w, R1h и R2x, R2y, R2w, R2h при создании pygame.Rect объектов playerRect соответственно playerRect2

playerRect = pygame.Rect(R2x,R2y,R2w, R2h)
playerRect2 = pygame.Rect(R1x,R1y,R1w, R1h)

playerRect = pygame.Rect(R1x, R1y, R1w, R1h)
playerRect2 = pygame.Rect(R2x, R2y, R2w, R2h)
0 голосов
/ 19 марта 2020

исправил ваши проблемы:

# import the necessary modules
import pygame
import sys
import time

#initialize pygame
pygame.init()
screenSize=(700,500)
screen=pygame.display.set_mode((screenSize), 0)

# set the caption for the screen
pygame.display.set_caption("Meryem's Pong Game")

# define colours you will be using
WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
YELLOW = (255,255,0)

# set the size for the surface (screen)
screen_h = screen.get_height()
screen_w = screen.get_width()
cx = int(screen_w/2)
cy = int(screen_h/2)

#initialize variables for player
#variables for first rectangle
R1x = 660
R1y = 300
R1w = 10
R1h = 132

R1dx = 0
R1dy = 0
R1_score = 0

#variables for second rectangle
R2x = 10
R2y = 2
R2w = 10
R2h = 132

R2dx = 0
R2dy = 0
R2_score = 0

#ball variables
bx = cx
by = cy
dby = 1
dbx = 1
br = 5
cy =  screen.get_height()/2
cx =  screen.get_width()/2

# variable for scores
R1_score = 0
R2_score = 0

playerRect = pygame.Rect(R2x,R2y,R2w, R2h)
playerRect2 = pygame.Rect(R1x,R1y,R1w, R1h)
ballRect = pygame.Rect (cx,cy,30,30)

#speed
speed = 3

fontsize = 50
fontScore = pygame.font.SysFont('arial', 50)
fontScore = pygame.font.SysFont('arial', 50)

R1Score = fontScore.render(str(R1_score), True, (WHITE))
R2Score = fontScore.render(str(R2_score), True, (WHITE))

# speed of object "clock"
clock = pygame.time.Clock()

FPS_easy = 30 # set frames per second for easy level
FPS_medium = 80 # set frames per second for medium level
FPS_progressive = 100 # set frames per second for progressive level

# set main loop to True so it will run
main = True
# main loop
while main:
    for event in pygame.event.get(): # check for any events (i.e key press, mouse click etc.)
        if event.type ==pygame.QUIT: # check to see if it was "x" at top right of screen
            main = False         # set the "main" variable to False to exit while loop
        if event.type ==pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                R2dx = 0
                R2dy = -speed
            elif event.key == pygame.K_DOWN:
                R2dx = 0
                R2dy = speed
            if event.key == pygame.K_w:
                R1dx = 0
                R1dy = -speed
            elif event.key == pygame.K_s:
                R1dx = 0
                R1dy = speed
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                R2dx = 0
                R2dy = 0
            elif event.key == pygame.K_w or event.key == pygame.K_s:
                R1dx = 0
                R1dy = 0

    playerRect.y = playerRect.y + R1dy
    playerRect2.y = playerRect2.y + R2dy
    ballRect.move_ip(dbx,dby)


    #collision of ball
    if ballRect.top <= 0:
        dby = -dby
    if ballRect.bottom >= screen_h:
        dby = -dby

    if ballRect.left <= 0:
        dbx = -dbx
    if ballRect.right >= screen_w:
        dbx = -dbx

    if ballRect.colliderect(playerRect2):
        dbx = -dbx
    if ballRect.colliderect(playerRect):
        dbx = -dbx

    screen.fill(BLACK)

    # draw the shapes, in this case the blue rectangles
    pygame.draw.rect(screen, WHITE,(playerRect),0)
    pygame.draw.rect(screen, WHITE,(playerRect2),0)
    pygame.draw.circle(screen, RED,ballRect.center,br,0)
    screen.blit(R1Score, (280,10))
    screen.blit(R2Score, (400,10))

    # we are using .flip() here,  it basically works the same as .update()
    # we will discuss this more in class (you can use either one)
    time.sleep(0.005)
    pygame.display.flip()

# quit pygame and exit the program (i.e. close everything down)
pygame.quit()
sys.exit()

должен сказать, что код, который вы дали, не очень хорош

...