-2
\$\begingroup\$

so I am trying to make my first working game and it is not working. There is no error message, it just does nothing. I want the box guy to move right or left when you hold down left or right arrow key, it was working before, but you could not hold it, you would have to spam the button. now nothing happens can u help.

import pygame, sys
import time
from threading import Thread
pygame.init()
display_width = 800
display_height = 600
green = (0, 255, 0)
blue = (0, 0, 255)
greenblue = (0, 200, 255)
black = (0,0,0)
white = (255,255,255)
pygame.init()
x =  (display_width * 0.45)
y = (display_height * 0.8)
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
clock = pygame.time.Clock()
pygame.display.set_caption('game1(name later)')
skyimg =  pygame.image.load('sky.png')
boxImg = pygame.image.load('Bbigbox.png')
x_change = 0
leftpress = False
rightpress = False
def loadbox(x,y):
    gameDisplay.blit(boxImg, (x,y))


px = 40

py = 40





def new():
    if event.type == pygame.QUIT:
                pygame.quit()
                pg.quit()
                sys.exit()
                stop()
def keycheck():
    global px
    if leftpress == True:
        px -= 5
    elif rightpress == True:
        px += 5

def main():
    global px
    global leftpress
    global rightpress
    while True:

        for event in pygame.event.get():

            if event.type == pygame.KEYDOWN:









                        if event.key == pygame.K_LEFT:
                            if px < 800 and px > 0:
                                leftpress == True
                        elif event.key == pygame.K_RIGHT:
                            if x < 800 or x > 0:
                                rightpress == True

                        if event.key == pygame.K_SPACE:
                            print("test")

            if event.type == pygame.KEYUP:


                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:

                    leftpress = False
                    rightpress = False


        keycheck()
        gameDisplay.fill(greenblue)
        loadbox(px, py)
        pygame.display.update()
main()

pictures and code: https://drive.google.com/open?id=1-uu4wk2JMG-OjSHoXBkBtuHOxq1GEBO1

\$\endgroup\$
0
0
\$\begingroup\$

I fixed it this is the part of my code I changed

                if event.key == pygame.K_LEFT:

                    xchange += -10
                elif event.key == pygame.K_RIGHT:
                    xchange += 10



        if event.type == pygame.KEYUP:
            xchange = 0






    print(xchange)
    px += xchange
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.