检查圆圈是否位于窗户的边缘



我目前正在尝试制作乒乓球游戏,但我目前正在努力挣扎。如果屏幕的边缘的边缘,我需要使球弹跳,但是由于某种原因,它会继续前进并且无法检测到碰撞。这是我的代码:

import sys, pygame, random
pygame.init()
width, height = 1200, 675
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
clock = pygame.time.Clock()

FPS = 120
xmb1 = False
xmf1 = False
ymb1 = False
ymf1 = False
xmb2 = False
xmf2 = False
ymb2 = False
ymf2 = False
squareh = 275
squarew = 35
squares = 3
x1 = 75
y1 = (height / 2) - (squareh / 2)
x2 = width - 75 - squarew
y2 = (height / 2) - (squareh / 2)
BLACK = (0,0,0)
WHITE = (255,255,255)
font = pygame.font.SysFont("arial", 30)
text = font.render("Press Space to start", True, WHITE)
text3 = font.render("3", True, WHITE)
text2 = font.render("2", True, WHITE)
text1 = font.render("1", True, WHITE)
startt = font.render("Start!", True, WHITE)
text3b = False
text2b = False
text1b = False
starttb = False
start = False
startballmove = False
bx = width / 2
by = height / 2
br = 40
bxm = random.randint(-6, 6)
bym = random.randint(-6, 6)
btc = by
blc = bx
bbc = by + br + br
brc = bx + br + br
circle=pygame.Surface((br * 2, br  * 2))
circle.fill((0, 0, 0))
pygame.draw.circle(circle, WHITE , (br, br), br, 0)
circle.set_colorkey((0, 0, 0))
while 1:
    if start and not text1b and not text2b and not text3b and not starttb and not startballmove:
        text3b = True
        pygame.time.set_timer(pygame.USEREVENT, 1000)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.display.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            # pad 2 check if key is down
            if event.key == pygame.K_UP:
                ymb2 = True
            if event.key == pygame.K_DOWN:
                ymf2 = True
            # pad 1 check if key is down
            if event.key == pygame.K_w:
                ymb1 = True
            if event.key == pygame.K_s:
                ymf1 = True
            if event.key == pygame.K_SPACE:
                start = True
        elif event.type == pygame.KEYUP:
            #pad 2 check if key goes up
            if event.key == pygame.K_UP:
                ymb2 = False
            if event.key == pygame.K_DOWN:
                ymf2 = False
            # pad 1 check if key goes up
            if event.key == pygame.K_w:
                ymb1 = False
            if event.key == pygame.K_s:
                ymf1 = False
        # check if window has been resized
        if event.type == pygame.VIDEORESIZE:
            width = event.dict['size'][0]
            height = event.dict['size'][1]
            screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
        if event.type == pygame.USEREVENT:
            # check if start should be hidden
            if starttb:
                starttb = False
                startballmove = True
            # check if start should be showed
            if text1b and not text2b and not text3b:
                text1b = False
                text2b = False
                text3b = False
                starttb = True
            # check if 1 should be showed
            if text2b and not text3b and not text1b:
                text3b = False
                text2b = False
                text1b = True
            # check if 2 should be showed
            if text3b and not text2b and not text1b:
                text3b = False
                text2b = True
                text1b = False
    # check if pad 1 is out of bounds and move it      
    if ymb1 and not (y1 <= 0): y1 -= squares
    if ymf1 and not (y1 + squareh >= height): y1 += squares
    if y1 > (height - squareh) + 1: y1 -= squares
    # check if pad 2 is out of bounds and move it      
    if ymb2 and not (y2 <= 0): y2 -= squares
    if ymf2 and not (y2 + squareh >= height): y2 += squares
    if y2 > (height - squareh) + 1: y2 -= squares
    # put pads in center if game has not started
    if not start:
        # pad 1
        x1 = 75
        y1 = (height / 2) - (squareh / 2)
        # pad 2
        x2 = width - 75 - squarew
        y2 = (height / 2) - (squareh / 2)
        #ball
        bx = width / 2 - br
        by = height / 2 - br
    # put pads in center in x if game has started
    else:
        # pad 1
        x1 = 75
        # pad 2
        x2 = width - 75 - squarew
    # if ball has not started moving center it
    if not startballmove:
        bx = width / 2 - br
        by = height / 2 - br
    # check if movement variables are 0
    while bxm == 0 or bym == 0:
        if bxm == 0:
            bxm = random.randint(-6, 6)
        if bym == 0:
            bym = random.randint(-6, 6)
    screen.fill(BLACK)
    # draw starting text if game has not started
    if not start:
        screen.blit(text,((width / 2) - text.get_width() // 2, (height / 4) - text.get_height() // 2))
    # put 3 on screen
    if start and text3b:
        screen.blit(text3,((width / 2) - 15, (height / 4) - (text.get_height() / 2)))
    # put 2 on screen
    if start and text2b:
        screen.blit(text2,((width / 2) - 15, (height / 4) - (text.get_height() / 2)))   
    # put 1 on screen
    if start and text1b:
        screen.blit(text1,((width / 2) - 15, (height / 4) - (text.get_height() / 2)))
    # put start on screen
    if start and starttb:
        screen.blit(startt,((width / 2) - (text.get_width() / 8), (height / 4) - (text.get_height() / 2)))   
    # check if ball is out of bounds
    if start and startballmove:
        if btc <= 0:
            by = by * -1
            print("top side")
        if bbc >= height:
            by = by * -1
            print("bottom side")
        if blc <= 0:
            bx = bx * -1
            print("left side")
        if brc >= width:
            bx = bx * -1
            print("right side")
    # move ball
    if start and startballmove:
        bx += bxm
        by += bym
    # draw circle if game start
    if start:
        screen.blit(circle, (int(bx), int(by)))  
    # draw pad 1
    pygame.draw.rect(screen, WHITE, (x1, y1, squarew, squareh), 0)
    # draw pad 2
    pygame.draw.rect(screen, WHITE, (x2, y2, squarew, squareh), 0)
    pygame.display.flip()
    clock.tick(FPS)

有人知道为什么这不起作用吗?我刚刚学习了pygame。

您检查的变量是否从未更改过"球超出界限"(因此,评论,谢谢!)。E. g。 btc在循环外的开头分配了一次,然后再也不会分配。

我想您应该在by(等)更改时更改它们。

当然,这不是一个好的编码样式。但是试图修复这将打开整个蠕虫; - )

编辑:此外,您应该否定速度变量,而不是位置变量i。e。bym代替bybym = bym * -1。解决这两个问题后,我的球不断从窗户上的所有边缘弹起。但是,踏板还没有影响:

    # check if ball is out of bounds
    btc = by
    blc = bx
    bbc = by + br + br
    brc = bx + br + br
    if start and startballmove:
        if btc <= 0:
            bym = bym * -1
            print("top side")
        if bbc >= height:
            bym = bym * -1
            print("bottom side")
        if blc <= 0:
            bxm = bxm * -1
            print("left side")
        if brc >= width:
            bxm = bxm * -1
            print("right side")

相关内容

  • 没有找到相关文章

最新更新