Python scripts converted from Students' Pascal Programs
The first example of a Python script that we based on the Pascal program Rocket shows an example of motion graphics on the console. You can try it online here.
# Pascal original copyright (c) 2010 Alkesh Gajjar # Licensed under the Apache License, Version 2.0 (the "License"); you may not # use this file except in compliance with the License, as described at # http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/ # Converted to Python by PPS from time import sleep from os import system system('cls') # Windows print('\n\n\n\n\n\n') print(' /*\ ') print(' /***\ ') print(' /*****\ ') print(' |*******| ') print(' |*******| ') print(' |*******| ') print(' |*******| ') print(' |*******| ') print(' |*******| ') print(' |*******| ') print(' |*******| ') print(' |*******| ') print(' / \ ') print(' / \ ') print(' /_____\ ') print(' *** ') print(' (*****) ') print(' {*******} \n') for i in range(1, 40): sleep_time = 1000 / i sleep(sleep_time / 1000) print('')
Functions
This example is based on the Pascal program Functions. Coloured output to the console is achieved with Colorama, for which we provide straightforward installation instructions.
# Pascal original of Functions copyright (c) 2010 Heemesh Vara # Licensed under the Apache License, Version 2.0 (the "License"); you may not # use this file except in compliance with the License, as described at # http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/ # Converted to Python by PPS def user_choice(): global choice choice = input('\nPlease enter the number of your chosen option. ') if choice == '1': display_date() elif choice == '2': displayASCII() elif choice == '3': display_reverse_string() elif choice == '4': display_character() elif choice == '5': quit() def display_date(): today = datetime.date.today() print(today.strftime("%d/%m/%Y")) def displayASCII(): my_char = input('Please type the character. ') print('\nIts ASCII value is ', ord(my_char), '.', sep='') def display_reverse_string(): my_string = input('Please type in a word or sentence. ') print('\nThe word or sentence reversed is ', my_string[::-1], '.', sep='') def display_character(): str_my_code = input('Please type in the ASCII code. '); my_code = int(str_my_code) print('\nThis represents ', chr(my_code), '.', sep=''); def quit(): print('\nI hope you found this useful.') def show_menu(): print('\n1: The Date') print('2: The ASCII code for a letter') print('3: Reverse Word') print('4: The character for an ASCII code') print('5: Quit the program') # main section from colorama import init, Back import datetime init() print(Back.RED, end='') choice = 0 while choice != '5': show_menu() user_choice()
Snake
This script uses UniCurses, for which we provide installation instructions.
# Pascal version of Snake copyright (c) 2010 Peter Hearnshaw # Licensed under the Apache License, Version 2.0 (the "License"); you may not # use this file except in compliance with the License, as described at # http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/ # Converted to Python by PPS from unicurses import * from time import sleep from random import randint stdscr = initscr() # initializes the UniCurses standard screen charX, charY, xInc, yInc, foodX, foodY, lx, SnakeLen = 0, 0, 1, 0, 0, 0, 0, 3 finish = False myArrayX = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] myArrayY = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] addstr('This program will make a snake game. However on this version you can not die.\n'); sleep(1.5) addstr('Use the arrow keys to move and x to quit\n') sleep(1.5) addstr('Press enter to start the game\n ') cbreak() # Gets raw key inputs but allows CRTL+C to work keypad(stdscr, True) # Get arrow keys etc. noecho() # Do not display automatically characters for key presses dummy = getch() curs_set(0) # Turns off cursor foodX = randint(0, 79) # foodX becomes a random number from 0 to 79 foodY = randint(0, 24) # foodY becomes a random number from 0 to 24 while not finish: clear() charX = charX + xInc # head of snake increases in X by xInc charY = charY + yInc # head of snake increases in Y by yInc # If the snake is off the screen in any of the four sides # the snake head will move to the opposite side. if charY == -1: charY = 24 if charY == 25: charY = 0 if charX == -1: charX = 79 if charX == 80: charX = 0 # array contains all positions the head of the snake has been on previous repeats # insert the new snake head value and shuffle all the other other body segment positions # along one and the end body segment will disappear myArrayX[SnakeLen] = charX myArrayY[SnakeLen] = charY for lx in range(0, SnakeLen): myArrayX[lx] = myArrayX[lx + 1] myArrayY[lx] = myArrayY[lx + 1] # For all the values in the array display an O as a body segment. for lx in range(0, SnakeLen + 1): mvaddch(myArrayY[lx], myArrayX[lx], ord('O')) if not ((myArrayY[lx] == foodY) and (myArrayX[lx] == foodX)): mvaddch(foodY, foodX, ord('9')) # Draw food if not eaten timeout(50) cha = getch() # Variable cha holds code of pressed key (ERR if timeout exceeded) if cha == KEY_UP: yInc = -1 # yInc and xInc change the direction of the snake xInc = 0 elif cha == KEY_DOWN: yInc = 1 xInc = 0 elif cha == KEY_LEFT: xInc = -1 yInc = 0 elif cha == KEY_RIGHT: xInc = 1 yInc = 0 elif cha == ord('x'): clear() refresh() curs_set(1) finish = True # if the snake head is on the food position if (charX == foodX) and (charY == foodY): # then move the food position foodX = randint(0, 79) foodY = randint(0, 24) # and make the snake longer if not (SnakeLen > 20): SnakeLen = SnakeLen + 1