【问题标题】:My paddles move by opposite keys the left paddle moves with arrows while the right paddle moves with wsad. How can I fix that?我的桨通过相反的键移动,左桨用箭头移动,而右桨用 wsad 移动。我该如何解决?
【发布时间】:2020-03-19 14:34:15
【问题描述】:

我正在复制一个基本的乒乓球游戏,而我的球拍是相反的。我的左桨随着箭头移动,而右桨随着 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()

【问题讨论】:

  • 您希望我们为您提供什么帮助

标签: python pygame collision


【解决方案1】:

解决了您的问题:

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

必须说你给出的代码不是很漂亮

【讨论】:

    【解决方案2】:

    当你创建pygame.Rect对象@987654330时,你混淆了R1xR1yR1hR2xR2yR2wR2h 987654331@

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 2022-01-23
      相关资源
      最近更新 更多