Sunday, April 02, 2017

Python Programming - Concept of object movement using key arrow / WASD

Game Preview

Pygame is look like a cartesius diagram that flipped vertically. Thats mean the Y-axis positive is on bottom and the negative Y-axis is on the top side. So the Pygame coordinate will look like this image.

Pygame Coordinate
From thats coordinate now we understand about how a object should do to move UP, DOWN, RIGHT, and LEFT. In this syntax i will shown to you how to make an object move using arrow or WASD keys.
here is the syntax:



Now I will explain as many as i can.
  •  Basic syntax   
You need to know about drawing circle, making a workspace, whats will happen when the key pressed, how to make color, and other. I dont need to describe this because you can learn about it in other basic post.
  • Movement 
    Xcoord = (playerCoord[A]['x']) #This to for shorter the player X coordinate
    Ycoord = (playerCoord[A]['y']) #This to for shorter the player Y coordinate
    if move == STOP:
        newCoord = {'x':Xcoord, 'y':Ycoord}  #Dont add a move while looping
    elif move == UP and Ycoord>25:
        newCoord = {'x':Xcoord, 'y':Ycoord-2} #For each looping the Y coordinate is reduction by 2 point so the object will move to up
    elif move == DOWN and Ycoord<ywindow-25:
        newCoord = {'x':Xcoord, 'y':Ycoord+2} #For each looping the Y coordinate is added by 2 point so the object will move to down
    elif move == LEFT and Xcoord>25:
        newCoord = {'x':Xcoord-2, 'y':Ycoord} #For each looping the X coordinate is reduction by 2 point so the object will move to left
    elif move == RIGHT and Xcoord<xwindow-25:
        newCoord = {'x':Xcoord+2, 'y':Ycoord} #For each looping the X coordinate is added by 2 point so the object will move to right
    else:
        newCoord = {'x':Xcoord, 'y':Ycoord}  #Dont add a move while looping same as stop

I think that all a concept to create this basic movement. For more information you can ask me in comment. Thanks for visiting ^^

0 comments:

Post a Comment