1

I wrote this code but it didn't work , I mean the window went unresponding :(.Please help!! Python_Army;) Is the problem my laptop or my laptop or is it the code and if you could improve my code please answer me as fast as possible!!

here is the code:

import pygame

pygame.init()

display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)


gameDisplay = pygame.display.set_mode((display_width,display_height))
background = pygame.image.load('C:/Users/H.S/Desktop/background.png')
player = pygame.image.load('C:/Users/H.S/Desktop/player.png')
file = pygame.image.load('C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg')
pygame.display.set_icon((file))
pygame.display.set_caption('a bit racey')
clock = pygame.time.Clock()

gameDisplay.blit(background, (0,0))
x = 300
y = 500
c = input()
if c == a:
    x-=1
    y = 500
    pygame.display.flip()
    pygame.display.update()
    gameDisplay.blit(player, (x,y))
elif c == d:
    x+=1
    y = 500
    pygame.display.update()
    pygame.display.flip()
    gameDisplay.blit(player, (x,y))
elif c == w:
    y+=1
    x = 300
    pygame.display.flip()
    pygame.display.update()
    gameDisplay.blit(player, (x,y))
elif c == s:
    y-=1
    x = 300
    pygame.display.flip()
    pygame.display.update()
    gameDisplay.blit(player, (x,y))
crashed = False

while not crashed:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Just a tip: Try to not copy and paste your entire code into a question, and make it more specific so it is easier for the community to identify the problem. If you really need to show your code for testing purposes, then link it to somewhere else like GitHub. Thanks – RIPPLR May 13 '20 at 14:07

1 Answers1

2

You have to move and draw the player in the application loop. And of course you have to update the display in the application loop. The main application loop has to:

Use pygame.key.get_pressed() to get the states of the keys:

import pygame
pygame.init()

display_width, display_height = 800, 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)

gameDisplay = pygame.display.set_mode((display_width,display_height))
background = pygame.image.load('C:/Users/H.S/Desktop/background.png')
player = pygame.image.load('C:/Users/H.S/Desktop/player.png')
file = pygame.image.load('C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg')
pygame.display.set_icon((file))
pygame.display.set_caption('a bit racey')
clock = pygame.time.Clock()

x, y = 300, 500
crashed = False
while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        x -= 1
    if keys[pygame.K_d]:
        x += 1
    if keys[pygame.K_w]:
        y -= 1
    if keys[pygame.K_s]:
        y += 1

    gameDisplay.blit(background, (0,0))
    gameDisplay.blit(player, (x, y))
    pygame.display.flip()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174