0

I just started trying out pygame, so I watched a tutorial. When the guy showed, how to create edges left and right from the screen, it just didn't work when I did it, even though I've written it 100% equal as the tutorial guy. The tutorial is already two years old, so maybe pygame just has changed. Can somebody help me?

That's my code:

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)
  • first you could use `print()` to see which part of code is executed and what you have in variables. It is called `"print debuging"` and it helps to see what code is doing. – furas May 01 '22 at 01:44
  • maybe you should use `( )` when you run `(A or B) and not C` – furas May 01 '22 at 01:45
  • did you run in console to see error messages? It gives error because you have `presses` instead of `pressed` – furas May 01 '22 at 01:47
  • code `left_wall = pygame.draw.rect(...)` gives you Rect with `width=0` and later `colliderect()` doesn't see collision. You should define it as `left_wall = pygame.Rect(-2, 0, 2, 600)`. And the same for `right_wall` – furas May 01 '22 at 01:55
  • if you read/watch tutorial then in question (not in comments) you could add link to this tutorial. – furas May 01 '22 at 01:57

1 Answers1

1

The following code works for me after replacing the background.png path with a picture that exists on my computer. I changed left_wall and right_wall to call pygame.Rect instead of pygame.draw.rect and copied the draw.rect calls that were previously assigned to left_wall and right_wall to draw() function, and added some needed parentheses in two of the if statements. Also fixed a typo where pressed was spelled presses.

Without the parentheses, the character would always move right when right key is pressed even past the right edge of the window. When "d" is pressed, it would check against colliderect. Same for left arrow and "a" keys. or short-circuits, so if K_RIGHT or K_LEFT is pressed, it doesn't check the right hand side of the or in the if statements. With the parentheses added, the "move right" if statement always checks colliderect when K_RIGHT or K_d is pressed, instead of only when K_d is pressed.

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)
Nathan Mills
  • 2,243
  • 2
  • 9
  • 15