pygam中的壁碰撞检测

2024-06-25 06:53:48 发布

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

我正在开发一个类似于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()

其他的一切都差不多完成了,我在堆栈溢出中四处寻找,却找不到详细的答案。在

谢谢, 阿杰


Tags: orkeyeventhelloifdisplaypygameprint
3条回答

阅读游戏机.rect以及pygame.rect.colliderect在

你可以把墙做成矩形,把矩形放在保险杠上,然后检测保险杠何时与墙壁相撞。在

抱歉回答得太短了。在

你可以使用rects或者你可以使用大量的if语句。在

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

你要做的就是检查球的右边(xc+25)是否大于右边球拍的左边(x1)。左桨也一样。在

在pygame中使用rects,可以使用函数b.colliderect(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()

相关问题 更多 >