我很难让球拍留在屏幕上?

2024-05-29 02:48:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着让桨上下移动:1个矩形。使用w/a/s/d键2矩形移动。用箭。我很难让球拍留在屏幕上

您将制作两个“挡板”(即矩形),一个在屏幕左侧,一个在屏幕右侧。这些长方形应该比它们的宽度高(即,看起来像乒乓球比赛中的桨)。左侧的挡板应能使用w和s键上下移动,右侧的挡板应能使用上下箭头键上下移动。不允许两个挡板离开屏幕的顶部或底部。(*对于此任务,您只需创建桨并使其正确移动,无需球)。尽量避免硬编码值

# import the necessary modules
import pygame
import sys

#initialize pygame
pygame.init()

# set the size for the surface (screen)
screen_h = 800
screen_w = 600

screen = pygame.display.set_mode((screen_h,screen_w),0)

# set the caption for the screen
pygame.display.set_caption("Template")

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

#initialize variables for player
#variables for first rectangle
R1x = 740
R1y = 300
R1w = 50
R1h = 132
R1dx = 0
R1dy = 0

#variables for second rectangle
R2x = 10
R2y = 300
R2w = 50
R2h = 132
R2dx = 0
R2dy = 0

#speed
speed = 3

screen.fill(BLACK)

# 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


    # move the x and y positions of the rectangles
    oldR1x = R1x
    oldR1y = R1y

    R1x = R1x + R1dx
    R1y = R1y + R1dy

    if R1x >= screen_w-50:
        R1x = oldR1x
        R1y = oldR1y
    if R1x<= 50:
        R1x = oldR1x
        R1y = oldR1y
    if R1y>= screen_h-50:
        R1x = oldR1x
        R1y = oldR1y
    if R1y<= 50:
        R1x = oldR1x
        R1y = oldR1y

    # draw the shapes, in this case the blue rectangles
    pygame.draw.rect(screen, WHITE,(R1x, R1y, R1w, R1h),0)
    pygame.draw.rect(screen, WHITE,(R2x, R2y, R2w, R2h),0)

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

Tags: thekeyeventforifmainscreenpygame
1条回答
网友
1楼 · 发布于 2024-05-29 02:48:48

首先,您交换了屏幕宽度(screen_w)和屏幕高度(screen_h)。它必须是:

# set the size for the surface (screen)
screen_w = 800
screen_h = 600

screen = pygame.display.set_mode((screen_w,screen_h),0)

拨片只上下移动,因此足以将PDDEL的y坐标限制在[0,screen_h-R1h范围内。注意,R1y分别R2y是桨叶的顶部坐标:

R1y = max(0, min(screen_h-R1h, R1y + R1dy))
R2y = max(0, min(screen_h-R2h, R2y + R2dy))

此外,必须在主应用程序循环(screen.fill(BLACK))中清除屏幕:

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:
            # [...]

    # move the x and y positions of the rectangles
    R1y = max(0, min(screen_h-R1h, R1y + R1dy))
    R2y = max(0, min(screen_h-R2h, R2y + R2dy))

    # clear screen
    screen.fill(BLACK)

    # draw the shapes, in this case the blue rectangles
    pygame.draw.rect(screen, WHITE,(R1x, R1y, R1w, R1h),0)
    pygame.draw.rect(screen, WHITE,(R2x, R2y, R2w, R2h),0)

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

注意,如果桨叶也沿x轴(左、右)移动,则x坐标的限制类似:

R1x = max(0, min(screen_w-R1w, R1x + R1dx))
R2x = max(0, min(screen_w-R2w, R2x + R2dx))

相关问题 更多 >

    热门问题