0
votes

I just want to move a block left and right but don't now why the keydown code isn't getting through. When I open the program it just shows the the 'tank' in its position but you can't move it with left or right keys.

import pygame, sys
from pygame.locals import *

WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
TANK_SIZE = 20

BLACK = (0  ,0  ,0  )
WHITE = (255,255,255)

def drawArena():
    DISPLAYSURF.fill(BLACK)

def drawTank(tank):
    pygame.draw.rect(DISPLAYSURF, WHITE, tank)

def main():
    pygame.init()
    global DISPLAYSURF
    DISPLAYSURF = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
    pygame.display.set_caption('Tanks')

    tankX = 200
    tankY = 200

    tank = pygame.Rect(tankX, tankY, TANK_SIZE, TANK_SIZE)

    drawArena()
    drawTank(tank)

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

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    tankX -= 20
                if event.key == pygame.K_RIGHT:
                    tankX += 20

        drawArena()
        drawTank(tank)

        pygame.display.update()

if __name__ == '__main__':
    main()
2

2 Answers

0
votes

this is because updated tankX values do not affect the tank object. there are many ways to make it work. for example, inserting a re-initialization of tank in the while True loop:

import pygame, sys
from pygame.locals import *

WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
TANK_SIZE = 20

BLACK = (0  ,0  ,0  )
WHITE = (255,255,255)

def drawArena():
    DISPLAYSURF.fill(BLACK)

def drawTank(tank):
    pygame.draw.rect(DISPLAYSURF, WHITE, tank)

def main():
    pygame.init()
    global DISPLAYSURF
    DISPLAYSURF = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
    pygame.display.set_caption('Tanks')

    tankX = 200
    tankY = 200

    tank = pygame.Rect(tankX, tankY, TANK_SIZE, TANK_SIZE)

    drawArena()
    drawTank(tank)

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

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    tankX -= 20
                if event.key == pygame.K_RIGHT:
                    tankX += 20

        tank = pygame.Rect(tankX, tankY, TANK_SIZE, TANK_SIZE)

        drawArena()
        drawTank(tank)

        pygame.display.update()

if __name__ == '__main__':
    main()
0
votes

You should not update the tankX and tankY variables as this doesn't affect the tank Rect object. You don't need to re-initialize the tank object as this is probably a waste of resources. A more efficient way is to just directly update the x and y values of the tank object using it's move_ip() function.

In your main loop...

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

        if event.type == pygame.KEYDOWN:
            if event.type == pygame.K_LEFT:
                tank.move_ip(-20, 0)
            if event.type == pygame.K_RIGHT:
                tank.move_ip(20, 0)

    drawArena()
    drawTank(tank)

    pygame.display.update()