python - 我的直肠没有更新

标签 python python-3.x pygame

我已经使用 pygame 一段时间了,但现在玩家矩形没有更新或注意到碰撞。但它仍在移动。这是我修改了一些代码后发生的。我已经查看了 .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()

最佳答案

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

player1.topleft = (xmod, ymod)

关于python - 我的直肠没有更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51803098/

相关文章:

python - 在线工具和Python脚本有两个不同的AES密文结果?

python - 在与 Scrapy 进行身份验证的登录 session 后使用 Selenium

python - 可以评论 pylint 注释

python-3.x - 如何根据 Excel Dataframe 中的内容突出显示行?

python - 为什么我的 PyGame 混音器不播放声音?

python - 使用 Python 从 URL 获取子域

在早期版本上运行的 Python 可执行文件

python - 防止图像被清除[PYGAME-Python]

python - 如何获取利萨如曲线表中所有曲线的X和Y位置?

python - cx_oracle OSX 安装错误