【问题标题】:Wall collision detection in pygamepygame中的墙壁碰撞检测
【发布时间】:2013-12-27 08:43:08
【问题描述】:

我正在开发一个游戏,类似于 pygame 上的 Pong,我遇到了墙壁碰撞检测的问题。它适用于两个桨,但不适用于球本身。代码是有道理的,但实际上并不起作用。

import pygame, sys
from pygame.locals import *
x = 25
xc = 404
yc = 300
y = 225
x1 = 740
movey1 = 0
y1 = 225
movex = 0
movey = 0
movebx = 2
moveby = 2
WHITE = (255,255,255)
GREEN = (0,255,0)
BLUE = (0,0,128)
RANDOM = (255,0, 0)
BLACK = (0,0,0)
width = 600
pygame.init()
display = pygame.display.set_mode((800,600))
pygame.display.set_caption("Pong!")
img = pygame.image.load("pongbg.jpg")
pygame.mixer.music.load("stay.mp3")
pygame.mixer.music.play(0)

while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_m:
                pygame.mixer.music.load("stay.mp3")
                pygame.mixer.music.play(0)
            if event.key == pygame.K_w:
                movey = -2
                pygame.display.flip() 
                if y  <= 0 or y>=600:
                    print "hello"
                    movey = -movey
            if event.key == pygame.K_s:
                movey = 2
                pygame.display.flip()
                if y >= 600 or y <0:
                    print "hello"
                    movey = -movey
            if event.key == pygame.K_UP:
                movey1 = -2
                if y1  <= 0 or y1> 600:
                    print "hello"
                    movey1 = -movey1
            if event.key == pygame.K_DOWN:
                movey1 = 1.5 
                if y1  <= 0 or y1> 600:
                    print "hello"
                    movey1 = -movey1
            if yc < 0 or yc >= 600 or yc >= 500:
                print "hello"
                moveby = -moveby
            if xc < 0 or xc > 800:
                print "hello"
                moveby = -moveby
        if event.type == pygame.KEYUP:
            movey1 = 0
            movey = 0
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    x += movex
    y += movey
    y1 += movey1
    xc+=movebx
    yc+=moveby
    #xc += movey
    #yc +=movey1
    pygame.display.flip()
    display.blit(img, (0,0))
    asdf = pygame.draw.rect(display, RANDOM, (x,y,34, 154))
    ghjk = pygame.draw.rect(display,RANDOM, (x1,y1,34,154))
    qwerty = pygame.draw.circle(display,GREEN, (xc,yc), 25,0)
    pygame.display.flip()
    pygame.display.update()

其他的都差不多完成了,我在堆栈溢出中环顾四周,但找不到详细的答案。

谢谢, 阿杰

【问题讨论】:

    标签: python pygame collision detection


    【解决方案1】:

    阅读 pygame.rect 和 pygame.rect.colliderect

    您可以制作墙壁矩形并将矩形放在保险杠上,并检测保险杠何时与墙壁发生碰撞。

    对不起,简短的回答。

    【讨论】:

    【解决方案2】:

    您可以使用 rects,也可以使用大量 if 语句。

    你知道球的 x 和球的 y。你知道球的宽度和高度,还有球拍。

    您所要做的就是检查球的右侧 (xc+25) 是否大于右侧球拍的左侧 (x1)。与左桨相同。

    在 pygame 中使用 rects,您可以使用函数 b.colliderect(a),如果 b 和 a 发生碰撞,则返回 True。

    【讨论】:

    • 非常感谢您的有用建议!
    【解决方案3】:

    恐怕你的桨检测也不起作用。我的建议是将您的代码划分为类。这样,桨将被告知移动的意图,但他们将决定是否可以移动。我还注意到您正在事件循环内翻转屏幕。事件循环不应该更新屏幕,只移动精灵。

    你可以从这里开始:

    class Paddle:
        def __init__(self,x,y):
            self.vy = 0
            self.y = y
            self.x = x
        def move(self):
            if( self.vy == 1 and canMoveUp(self.y) or self.vy == -1 and canMoveDown(self.y):
                self.y+=vy
        def draw(self,screen):
            screen.blit()
    
    class Ball:
        def __init__(self,x,y):
            self.vx = 1
            self.vy = 0
            self.x = y
            self.y = x
        def move(self):
            #check for collisions same as with paddles
        def draw(self,screen):
            screen.blit()
    

    【讨论】:

    • 非常感谢您的有用建议!
    【解决方案4】:

    我只能说我的碰撞程序是如何工作的:

    import pygame
    from pygame.locals import*
    import sys
    
    pygame.init()
    
    screen = pygame.display.set_mode((1200, 700))
    
    ticket1 = True
    f = [0, 0]
    
    pa = 600
    pb = 650
    a = 600
    b = 650
    color = (100, 180, 100)
    while ticket1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                ticket1 = False
                pygame.quit()
                sys.exit()
    
    screen.fill((255, 250, 245))
    
    pygame.draw.circle(screen, color, (a, b), 50)
    pa += f[0]
    pb += f[1]
    a = int(pa)
    b = int(pb)
    if a-50 < 0:
        f[0] = -f[0]
        a = 51
        pa = 51
    elif a+50 > 1200:
        f[0] = -f[0]
        a = 1149
        pa = 1149
    
    if b-50 < 0:
        f[1] = -f[1]
        b = 51
        pb = 51
    elif b+50 > 700:
        f[1] = -f[1]
        b = 650
        pb = 650
    

    如果你认为这是一个不好的答案,这个程序可以工作,所以你可能不得不写

    if yc < 0 or yc >= 600 or yc >= 500:
                    print "hello"
                    moveby = -moveby
                if xc < 0 or xc > 800:
                    print "hello"
                    moveby = -moveby
    

    以外:

    for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
    

    但我不确定。

    对于桨,我认为之前的答案是个好主意。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-21
      • 1970-01-01
      • 2016-04-24
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多