我的矩形未更新

时间:2018-08-11 19:34:57

标签: python python-3.x pygame

我已经使用pygame一段时间了,但是现在玩家rect并没有更新或注意到碰撞。它仍然在移动。这只是在我修改了一些代码之后才发生的。我已经看过.draw函数,正在绘制墙壁和边框,但是没有播放器,并且正在更新屏幕。 (该代码段位于javascript中,因为我仍然不知道如何在python中使用该代码段。)

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

def render():
    windowSurface.fill(black)
    pygame.draw.rect(windowSurface,white,player1)
    if level == 1:
        pygame.draw.rect(windowSurface,white,wall_lvl_1_1)
        pygame.draw.rect(windowSurface,white,wall_lvl_1_2)
        pygame.draw.rect(windowSurface,white,border1_lvl_1)
    elif level == 2:
        filler = 'done'
    pygame.display.update()
black = (0,0,0)
white = (255,255,255)
windowSurface = pygame.display.set_mode((500, 500),0,32)
windowSurface.fill(black)
level = 1
xmod = 0
ymod = 0
direction = 'none'
player1 = pygame.Rect(225 + xmod,450 - ymod,30,30)
wall_lvl_1_1 = pygame.Rect(0,225,250,50)
wall_lvl_1_2 = pygame.Rect(300,250,200,50)
border1_lvl_1 = pygame.Rect(0,0,25,500)
border2_lvl_1 = pygame.Rect(0,0,500,25)
border3_lvl_1 = pygame.Rect(0,0,1,1)
pygame.draw.rect(windowSurface,white,wall_lvl_1_1)
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 player1.colliderect(wall_lvl_1_1) or player1.colliderect(wall_lvl_1_2):
        xmod = 0
        ymod = 0
        player1 = pygame.Rect(225 + xmod,450 - ymod,30,30)
        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)
    if ymod == 450:
        level = 2
    render()

1 个答案:

答案 0 :(得分:2)

您需要每帧更新player1的位置。

player1.topleft = (xmod, ymod)