Pygame的碰撞并没有注意到碰撞

时间:2018-08-08 09:40:21

标签: python python-3.x pygame

在此之前,我使用代码说“每个按钮按下它会移动”,但现在我将其更改为“当您按下按钮时它会向一个方向移动,直到进一步说明”,但是现在collectrect无效。它以前曾经有用,但我对pygame还是陌生的。我发现了一些类似的问题,但我想我已经完成了提出问题的人们所做的事情。任何帮助都可以接受。

import pygame, sys, random
from pygame.locals import *
from time import sleep
pygame.init()

def render():
    windowSurface.fill(black)
    player = pygame.Rect(225 + xmod,450 - ymod,30,30)
    pygame.draw.rect(windowSurface,white,player)
    pygame.draw.rect(windowSurface,white,wall_1)
    pygame.draw.rect(windowSurface,white,wall_2)
    pygame.display.update()

black = (0,0,0)
white = (255,255,255)
windowSurface = pygame.display.set_mode((500, 500),0,32)
windowSurface.fill(black)
xmod = 0
ymod = 0
direction = 'none'
player = pygame.Rect(225 + xmod,450 - ymod,30,30)
wall_1 = pygame.Rect(0,225,250,50)
wall_2 = pygame.Rect(300,250,200,50)
render()

while True:
    render()
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                direction = 'left'
            if event.key == K_RIGHT:
                direction = 'right'
            if event.key == K_UP:
                direction = 'up'
            if event.key == K_DOWN:
                direction = 'down'
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    if player.colliderect(wall_1) or player.colliderect(wall_2):
        xmod = 0
        ymod = 0
        player = pygame.Rect(225 + xmod,450 - ymod,30,30)
        render()
        print('again')
    if direction == 'left':
        xmod -= 1
        sleep(0.004)
    if direction == 'right':
        xmod += 1
        sleep(0.004)
    if direction == 'up':
        ymod += 1
        sleep(0.004)
    if direction == 'down':
        ymod -= 1
        sleep(0.004)

1 个答案:

答案 0 :(得分:1)

您永远不会更新全局player变量,因此它会保持其原始坐标。在render函数中,创建一个新的rect并将其分配给本地player变量,但它与用于冲突检测的全局player不同。

我建议为速度speed_x, speed_y添加变量并将其添加到每帧的player.x.y属性中,以直接移动矩形。

player函数中的本地render可以删除。

import sys
import pygame
from pygame.locals import *


pygame.init()

def render():
    windowSurface.fill(black)
    pygame.draw.rect(windowSurface, white, wall_1)
    pygame.draw.rect(windowSurface, white, wall_2)
    pygame.draw.rect(windowSurface, white, player)
    pygame.display.update()


black = (0,0,0)
white = (255,255,255)
windowSurface = pygame.display.set_mode((500, 500),0,32)
clock = pygame.time.Clock()  # A clock to limit the frame rate.

player = pygame.Rect(225, 450, 30, 30)
wall_1 = pygame.Rect(0,225,250,50)
wall_2 = pygame.Rect(300,250,200,50)

speed_x = 0
speed_y = 0

while True:
    # Handle events.
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_LEFT:
                speed_x = -5
                speed_y = 0
            elif event.key == K_RIGHT:
                speed_x = 5
                speed_y = 0
            elif event.key == K_UP:
                speed_y = -5
                speed_x = 0
            elif event.key == K_DOWN:
                speed_y = 5
                speed_x = 0

    # Add the speed to the x and y attributes to move the rect.
    player.x += speed_x
    player.y += speed_y

    # Game logic.
    if player.colliderect(wall_1) or player.colliderect(wall_2):
        player = pygame.Rect(225, 450, 30, 30)
        print('again')

    # Render everything.
    render()

    clock.tick(60)  # Limit the frame rate to 60 FPS.