Hacking
python game code script free download
python game code script
def print_board(board): print "The board look like this: \n" for i in range(3): print " ", for j in range(3): if board[i*3+j] == 1: print 'X', elif board[i*3+j] == 0: print 'O', elif board[i*3+j] != -1: print board[i*3+j]-1, else: print ' ', if j != 2: print " | ", print if i != 2: print "-----------------" else: print def print_instruction(): print "Please use the following cell numbers to make your move" print_board([2,3,4,5,6,7,8,9,10]) def get_input(turn): valid = False while not valid: try: user = raw_input("Where would you like to place " + turn + " (1-9)? ") user = int(user) if user >= 1 and user <= 9: return user-1 else: print "That is not a valid move! Please try again.\n" print_instruction() except Exception as e: print user + " is not a valid move! Please try again.\n" def check_win(board): win_cond = ((1,2,3),(4,5,6),(7,8,9),(1,4,7),(2,5,8),(3,6,9),(1,5,9),(3,5,7)) for each in win_cond: try: if board[each[0]-1] == board[each[1]-1] and board[each[1]-1] == board[each[2]-1]: return board[each[0]-1] except: pass return -1 def quit_game(board,msg): print_board(board) print msg quit() def main(): # setup game # alternate turns # check if win or end # quit and show the board print_instruction() board = [] for i in range(9): board.append(-1) win = False move = 0 while not win: # print board print_board(board) print "Turn number " + str(move+1) if move % 2 == 0: turn = 'X' else: turn = 'O' # get user input user = get_input(turn) while board[user] != -1: print "Invalid move! Cell already taken. Please try again.\n" user = get_input(turn) board[user] = 1 if turn == 'X' else 0 # advance move and check for end game move += 1 if move > 4: winner = check_win(board) if winner != -1: out = "The winner is " out += "X" if winner == 1 else "O" out += " :)" quit_game(board,out) elif move == 9: quit_game(board,"No winner :(") if __name__ == "__main__": main()
Example code and programs
1.1 Text examples
- Program that calculates miles-per-gallon.
calculate_miles_per_gallon.py - Calculates the kinetic energy of an object.
calculate_kinetic_energy.py - Simple example if statements.
if_statement_examples.py - Simple example for loops.
for_loop_examples.py - Simple example while loops. (Chapter 4
while_loop_examples.py - Simple example for encrypting text.
simple_encryption.py - Simple example for decrypting text.
simple_decryption.py - Binary and linear searches.
searching_example.py - Property check examples.
property_check_examples.py
1.2 Graphics examples
- This opens up a blank pygame window. It is a good template to use when starting a new program.
pygame_base_template.py - This file demonstrates setting up a window and drawing rectangles, polygons, text, and other basic shapes.
simple_graphics_demo.py - See how to rotate text.
text_rotate.py - Demos all the commands in the draw module.
draw_module_example.py - Animate a bouncing rectangle around the screen.
bouncing_rectangle.py - Animate a several bouncing balls.
bouncing_balls.py - This shows how to use an array to animate and track multiple objects. In this case, snow flakes.
animating_snow.py - This file draws a snowman inside of a function. The program can then call the draw snowman function and draw several snowmen easily.
functions_and_graphics.py - Move an object with the mouse.
move_mouse.py - Move an object with the keyboard.
move_keyboard.py - Move an object with the game controller.
move_game_controller.py - Show how to read everything off a gamepad/controller/joystick.
joystick_calls.py - Display bitmapped images (png, jpg) to a screen. Make sounds.
bitmapped_graphics.py - Display a board based on a two-dimensional grid. Useful when creating games like tic-tac-toe, minesweeper, memory-match, connect-four, etc.
array_backed_grid.py - Display one or more pages of instruction before the game starts.
instruction_screen.py - Plays background music. When the music is over, an event is triggered and a different song starts. The music is included in the downloadable zip file, or you can follow the links in the comments to download the music.
background_music.py - Shows how to display a centered "Game Over" message, and stop game play when a game is over.
game_over.py - Display a line sweeping around in a circle like a radar.
radar_sweep.py - Count up and count down timers.
timer.py - Using recursion to make nested rectangles.
recursive_rectangles.py - Using recursion to make fractals.
fractal.py - Put a timer on the screen.
timer.py
1.3 Sprite examples
- Move a sprite with the mouse and collect blocks.
sprite_collect_blocks.py - Same as collect blocks example, but all the sprites move.
moving_sprites.py - Same as collect blocks example, but you can move the sprite by clicking to pick them up.
pick_up_blocks.py - Rather than just move down, these sprites bounce.
moving_sprites_bounce.py - Prior example too boring? Get the sprites to move in circles.
sprite_circle_movement.py - Expands the prior example to show how to manage a game with levels. Level advances when all the blocks are cleared.
sprite_collect_blocks_levels.py - Same as sprite_collect_blocks.py but with a black circle instead of a block.
sprite_collect_circle.py - Same as sprite_collect_blocks.py but with a graphic instead of a block.
sprite_collect_graphic.py - Same as sprite_collect_blocks.py but with a graphic instead of a block.
sprite_rotating.py - Same idea as sprite_collect_blocks.py but uses a Game class to organize the code. This is a more advanced way of organizing the code. It helps if you want to “restart” a game once it is over.
game_class_example.py - Move a sprite around the screen with the mouse.
move_sprite_mouse.py - Move a sprite with the keyboard in discrete “jumps.”
move_sprite_keyboard_jump.py - Move a sprite with the keyboard smoothly and continuously while a key is pressed.
move_sprite_keyboard_smooth.py - Move a sprite with a game controller or joystick.
move_sprite_game_controller.py - Basic Pong game using two game controllers.
pong.py - How to manage bullets with sprites.
bullets.py - How to aim bullets.
bullets_aimed.py - Control a snake as it moves around the screen.
snake.py
1.4 Platformer examples
- Move a sprite around the screen, but not let it move through walls.
move_with_walls_example.py - More complex version of prior example, with multi-colored walls, and multiple rooms.
maze_runner.py - Get the player to jump off platforms.
platform_jumper.py - Like platform jumper, but scroll side to side.
platform_scroller.py - Make those platforms move!
platform_moving.py - A multi-file example that shows a platformer using sprite sheets.Sprite Sheets
Post a Comment
0 Comments