Python pygame中的墙碰撞检测
我在做一个类似于Pong on pygame的游戏,我遇到了墙壁碰撞检测的问题。它可以和两个桨一起工作,但不能和球一起工作。代码是有道理的,但实际上不起作用Python pygame中的墙碰撞检测,python,pygame,collision,detection,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
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()
import pygame,sys
从pygame.locals导入*
x=25
xc=404
yc=300
y=225
x1=740
移动1=0
y1=225
movex=0
movey=0
movebx=2
moveby=2
白色=(255255)
绿色=(0255,0)
蓝色=(0,0128)
随机=(255,0,0)
黑色=(0,0,0)
宽度=600
pygame.init()
display=pygame.display.set_模式((800600))
pygame.display.set_标题(“Pong!”)
img=pygame.image.load(“pongbg.jpg”)
pygame.mixer.music.load(“stay.mp3”)
pygame.mixer.music.play(0)
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K\m:
pygame.mixer.music.load(“stay.mp3”)
pygame.mixer.music.play(0)
如果event.key==pygame.K_w:
movey=-2
pygame.display.flip()
如果y=600:
打印“你好”
movey=-movey
如果event.key==pygame.K_:
movey=2
pygame.display.flip()
如果y>=600或y=600或yc>=500:
打印“你好”
moveby=-moveby
如果xc<0或xc>800:
打印“你好”
moveby=-moveby
如果event.type==pygame.KEYUP:
移动1=0
movey=0
如果event.type==退出:
pygame.quit()
sys.exit()
x+=movex
y+=移动
y1+=移动y1
xc+=movebx
yc+=移动人
#xc+=movey
#yc+=movey1
pygame.display.flip()
显示blit(img,(0,0))
asdf=pygame.draw.rect(显示,随机,(x,y,34154))
ghjk=pygame.draw.rect(显示,随机,(x1,y1,34154))
qwerty=pygame.draw.circle(显示器,绿色,(xc,yc),25,0)
pygame.display.flip()
pygame.display.update()
其他一切都差不多完成了,我在stack overflow中查看了一下,但找不到详细的答案
谢谢,
AJ了解pygame.rect和pygame.rect.collide rect 您可以将墙做成矩形,并将矩形放置在保险杠上,然后检测保险杠何时与墙碰撞
很抱歉回答得这么简短 您可以使用rect,也可以使用大量if语句 你知道球是x,球是y。你知道球的宽度和高度,还有球拍的高度 你所要做的就是检查球的右边(xc+25)是否大于右桨的左边(x1)。左桨也一样
对于pygame中的矩形,您可以使用函数b.collide rect(a),如果b和a发生碰撞,该函数将返回True。恐怕您的划桨检测也无法正常工作。我的建议是将代码分成几个类。这样,桨叶将被告知移动的意图,但它们将决定是否可以移动。我还注意到,您正在事件循环中翻转屏幕。事件循环不应更新屏幕,仅移动精灵 您可以从以下内容开始:
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()
我可以说我的程序是如何使用碰撞的:
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: