Hi, welcome to my personal blog. Hope you could find useful python tricks. Feel free to exchange your valuable ideas with me. I am looking forward to your insightful opinion.
In python, in operator will do the trick. >>> 'is' in 'What is this!' >>> True or you could use string method s.find() >>> s = 'What is this!' >>> s.find('is') >>> 5 >>> s.find('was') >>> -1 If find() found the substring, it returns the index where the substring first appears. Otherwise it returns -1. Under the hood, python will use __contains__(self, item), __iter__(self), and __getitem__(self, key) in that order to determine whether a substring is in a string.
The Python conditional expression is kinda like the ternary operator in other programming languages ( condition ? a : b ). Syntax: a if condition else b If condition evaluated to True, a is evaluated and returned, otherwise b is evaluated and returned. For instance, a = 1 b = 2 x = a if a < 0 else b In this case, if a < 0, x will be assigned with the value of a, however this is not the case, so the value of b is assigned to x. So x is 2 instead.
This game needs two files, first is the box_pusher.py file, second is the box_pusher_game_level.py file. I have added just 31 levels, there are another 200 levels. I wll add those levels later. Those two files need to be placed inside the same folder as the pictures below. Three pictures are all 20x20 pixels and need to be placed in the same folder as the source code. # box_pusher.py import pygame , sys from pygame.locals import * from box_pusher_game_level import game_level SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480 BOARD_WIDTH = 20 BOARD_HEIGHT = 16 CELL_SIZE = 20 FPS = 30 START_POS = ( 119 , 79 ) # color preset WHITE = ( 255 , 255 , 255 ) BLACK = ( 0 , 0 , 0 ) RED = ( 255 , 0 , 0 ) GREEN = ( 0 , 255 , 0 ) BLUE = ( 0 , 0 , 155 ) # game level dictionary # 'B' represent bricks # 'M' represent marks # 'O' represent boxes # 'P' represent player ##game_level = { ## 1: ( ## '......................
Comments
Post a Comment