pygame中的墙碰撞检测

时间:2013-12-27 08:43:08

标签: python pygame collision detection

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

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

其他一切都已完成,我已经在堆栈溢出中查看,但找不到详细的答案。

谢谢, AJ

4 个答案:

答案 0 :(得分:1)

阅读有关pygame.rect和pygame.rect.colliderect

的信息

你可以让墙壁反射并将保护装置放在保险杠上,并检测保险杠何时与墙壁碰撞。

对简短的回答感到抱歉。

答案 1 :(得分:0)

您可以使用rects,也可以使用if的语句加载。

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

你需要做的就是检查球是否正确(xc + 25)是否大于右桨(x1)。与左桨相同。

使用pygame中的rects,你可以使用函数b.colliderect(a),如果b和a碰撞,它返回True。

答案 2 :(得分:0)

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

您可以从以下内容开始:

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

答案 3 :(得分:0)

我可以说我的碰撞程序是如何工作的:

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:

但我不确定。

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