如何将屏幕限制在pygame?

How can I limit the screen in pygame?

我刚开始尝试 pygame,所以我看了教程。当那个人展示如何从屏幕上创建左右边缘时,我做的时候它就是不起作用,即使我已经把它写成 100% 与教程人一样。本教程已有两年历史,所以可能 pygame 只是发生了变化。有人可以帮助我吗?

这是我的代码:

import pygame
import sys

pygame.init()
background = pygame.image.load("C:\Programmieren\Python\Grafiken\pygame test1 - background.png")
screen = pygame.display.set_mode([1200,595])
clock = pygame.time.Clock()
pygame.display.set_caption("pygame test1")

def draw():
    screen.blit(background, (0, 0))
    pygame.draw.rect(screen, (0, 0, 255), (x, y, width, height))
    pygame.display.update()

x = 300
y = 300
speed = 3
width = 40
height = 80

left_wall = pygame.draw.rect(screen, (0,0,0), (-2,0,2,600), 0)
right_wall = pygame.draw.rect(screen, (0,0,0), (1201,0,2,600), 0)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

    character = pygame.Rect(x,y,width, height)
    pressed = pygame.key.get_pressed()
    if presses[pygame.K_UP] or pressed[pygame.K_w] or pressed[pygame.K_SPACE]:
        y -= speed
    if pressed[pygame.K_RIGHT] or pressed[pygame.K_d] and not character.colliderect(right_wall):
        x += speed
    if pressed[pygame.K_DOWN] or pressed[pygame.K_s]:
        y += speed
    if pressed[pygame.K_LEFT] or pressed[pygame.K_a] and not character.colliderect(left_wall):
        x -= speed

    draw()
    clock.tick(60)

将 background.png 路径替换为我计算机上存在的图片后,以下代码适用于我。我将 left_wall 和 right_wall 更改为调用 pygame.Rect 而不是 pygame.draw.rect 并复制了之前分配给 left_wall 和 [=22 的 draw.rect 调用=] 到 draw() 函数,并在两个 if 语句中添加了一些需要的括号。还修复了 pressed 拼写为 presses 的拼写错误。

如果没有括号,即使超过 window 的右边缘,当按下右键时字符总是向右移动。当按下“d”时,它会检查 colliderect。左箭头和“a”键相同。 or short-circuits,因此如果按下 K_RIGHT 或 K_LEFT,它不会检查 if 语句中 or 的右侧。添加括号后,“向右移动”if 语句总是在按下 K_RIGHT 或 K_d 时检查碰撞,而不是仅在按下 K_d 时检查。

import pygame
import sys

pygame.init()
background = pygame.image.load("C:\Programmieren\Python\Grafiken\pygame test1 - background.png")
screen = pygame.display.set_mode([1200,595])
clock = pygame.time.Clock()
pygame.display.set_caption("pygame test1")

def draw():
    screen.blit(background, (0, 0))
    pygame.draw.rect(screen, (0, 0, 255), (x, y, width, height))
    pygame.draw.rect(screen, (0,0,0), left_wall, 0)
    pygame.draw.rect(screen, (0,0,0), right_wall, 0)
    pygame.display.update()

x = 300
y = 300
speed = 3
width = 40
height = 80

left_wall = pygame.Rect(-2,0,2,600)
right_wall = pygame.Rect(1201,0,2,600)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

    character = pygame.Rect(x,y,width, height)
    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_UP] or pressed[pygame.K_w] or pressed[pygame.K_SPACE]:
        y -= speed
    if (pressed[pygame.K_RIGHT] or pressed[pygame.K_d]) and not character.colliderect(right_wall):
        x += speed
    if pressed[pygame.K_DOWN] or pressed[pygame.K_s]:
        y += speed
    if (pressed[pygame.K_LEFT] or pressed[pygame.K_a]) and not character.colliderect(left_wall):
        x -= speed

    draw()
    clock.tick(60)