Bestplatformer.py

import msvcrt

import time

import threading

#Note - fix world switcher.


###GAME LOGIC###

"""

The screen is built out of a list with each number representing the x value

+ the inverse of the y value * 20.


This means the pixel indexs represent the following pixels on the screen:

(Lowered down to a 5x5 rather than 20x10)


 01 02 03 04 05

 06 07 08 09 10

 11 12 13 14 15

 16 17 18 19 20

 21 22 23 24 25


For the ease of myself the pixel values are broken down into x and y

coordinaties, the x and y are packed up using a simple formula.

(Y x 20 x -1 + X) The minus one is used to make an increase

in value cause an increase, rather than a decrease, in height.

This is again for ease of use as it properly reflects regular

x and y graphs.


-


The input for the character currently works by running a

seperate process/thread to the main game, this prevents the

game only moving when a key is pressed an keeps a constant

game speed.


   (Process 1)

   

   FUNCTION keypress

       GLOBAL input

       

       input <- USERINPUT


   (Process 2)

   

   WHILE True

       game()

       

       sleepseconds(1)


An issue with the key detection is that it only detects key

presses, not key releases.


This means that the character will move one way until another key

is pressed; one way around the variable, in this case input

keeping the same char even after the key is released is to

redefine the variable, this means the key must be pressed

for every movement.


   (Process 1)

   

   FUNCTION keypress

       GLOBAL input

       input <- False

       

       input <- USERINPUT


Gravity works by testing to see if the block underneath

the character is a platform block and if it isn't it reduces

the players y value by one every time.


-


The world input works by reading a file and replacing every character

in the pixel list with it's corrisponding file counterpart.

So pixel index 4, becomes world file index 4.

"""


#PIXEL LIST BUILDER#

pixel = [" "]

for x in range(0,199):

   pixel.append(" ")


#COMPILES PIXEL LIST TO 20X10 SCREEN

def finalize():

   global Pixel

   final = ""

   xplus = 0

   for y in range(0,10):

       final = final + "\n"

       for x in range(0,20):

           final = final + pixel[xplus]

           xplus = xplus + 1

   print(final)


#ONE TIME FLIXIBLE GAME VARIABLES#

px = 1

py = 0

i = "INPUT"

debug = False


#SEPERATE PROCESS FOR KEY INPUT#

def keyinput():

   global i

   i = msvcrt.getwche()


#BLOCK ADD SIMPLIFIED#

def blockc(x,y,c):

   pixel[y*20*-1+x] = c


#BLOCK ADD WITH SPACES#

def blockd(x,y):

   pixel[y*20*-1+x] = " "


#WORLD COMPLIATION#

def world(wn):

   world = open(str(wn)+".txt","r")

   worldread = world.read()

   for x in range(0,200):

       pixel[x] = worldread[x]


#PLAYER/GAME MOVEMENT#

def game():

   global px

   global py

   global pixel

   global i

   global wn

   prevx = px

   prevy = py


   #clears boost figures

   for x in range(0,199):

       if pixel[x] == "}":

           pixel[x] = " "

       if pixel[x] == "{":

           pixel[x] = " "


   #Gets input for the character

   time.sleep(0.3)

   t = threading.Thread(target=keyinput)

   t.start()

   

   #Input chars to direction

   if i == "d":

       px = px + 1

   if i == "a":

       px = px - 1

       

   #TAP KEYS OR ICY KEYS

   #i = "INPUT"

   #Removing this activates icy keys, meaning momentem is kept.


   #Removes player blocks to prevent trail

   blockd(prevx,prevy)


   #Gravity

   if str(pixel[py*20*-1+px+20]) != "O":

       py = py - 1

       #Extra jump whilst in air.

       if i == "d":

           px = px + 1

           pixel[py*20*-1+px-1] = "{"

       if i == "a":

           px = px - 1

           pixel[py*20*-1+px+1] = "}"

   else:

       #Jump mech

       if i == "2ndjump":

           py = py + 2

           i = "INPUT"

       if i == "w":

           py = py + 3

           i = "2ndjump"


   #Compiles xy values to single pixel number: 0 - 199

   playerpos = py*20*-1+px


   #Worldmove

   if px > 19:

       wn = wn + 1

       px = 0

       world(wn)

   if px < 0:

       wn = wn - 1

       px = 19

       world(wn)


   #Sets player block

   pixel[playerpos] = "▓"    

   finalize()


#FUN LOADING THING#

load = ["-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-"]

for x in range(0,19):

   time.sleep(0.1)

   load[x] = "█"

   print("LOADING...")    

   print("".join(load))


wn = 0

world(0)

#GAMELOOP#

while 1:

   game()


   

   

You will need to create your own level files  which are just text files. Just ask me and I’ll be happy to help.