What framework are you working on? I don't think you have to do any collision detection. Let's say your tile's side is 30 pixels. When the players presses the left arrow key, for example, you'll tell the program to move the player spirte 30 px to the left at a rate of x pixels per frame, probably animating the sprite. You do need to raise a flag that the sprite is moving, so that other key presses are ignored.
This is my version of the solution, although I'm a terrible programmer, and it might be a little convoluted:
import pygame
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((800,600))
image = pygame.image.load('logo.png')
imagePos = [330,250]
running = True
movement = False
direction = ''
initialPos = imagePos[0]
def moveImageRight(movement,initialPos):
if movement == True:
if imagePos[0] != initialPos + 30:
imagePos[0] += 3
return True
initialPos = imagePos[0]
return False
def moveImageLeft(movement,initialPos):
if movement == True:
if imagePos[0] != initialPos - 30:
imagePos[0] -= 3
return True
initialPos = imagePos[0]
return False
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
running = False
key = pygame.key.get_pressed()
if key[K_RIGHT]:
if movement == False:
movement = True
direction = 'right'
initialPos = imagePos[0]
if key[K_LEFT]:
if movement == False:
movement = True
direction = 'left'
initialPos = imagePos[0]
if direction == 'right' and movement:
movement = moveImageRight(movement,initialPos)
if direction == 'left' and movement:
movement = moveImageLeft(movement,initialPos)
screen.fill((200,200,200))
for i in range(30):
pygame.draw.line(screen,(0,0,0),(30*i,0),(30*i,600),2)
screen.blit(image,tuple(imagePos))
pygame.display.flip()
pygame.quit()
I drew the lines just to check that the sprite won't move other than 30 pixels to either direction.