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.
First look at the following code: import time # create Font object text_font = pygame . font . SysFont( 'arial' , 50 ) # create a list that stores two pos that going to compare to each other pos_to_compare_list = [] # create a list that stores all the pos that matched pos_matched = [] # set backgroud color to white while True : for event in pygame . event . get(): if event . type == QUIT: pygame . quit() sys . exit() elif event . type == MOUSEBUTTONDOWN: (x,y) = pygame . mouse . get_pos() pos = check_click(x,y, square_pos) if pos: text_surface = text_font . render(board[pos[ 0 ]][pos[ 1 ]], True , WHITE) if len (pos_to_compare_list) < 2 : if pos not in pos_to_compare_list: pos_to_compare_list . append(pos) ...
Table of Contents Preparation Introduction Import modules and define contants Create the main game screen Creae the data structure of the board Let's draw the tiles The game logic Define the missing functions Preparation In this tutorial, we are gonna use Python 3.8 and Pygame v1.96. For the installation of Python and Pygame, please follow the documents found on each webiste. Python: https://www.python.org/ Pygame: https://www.pygame.org/ Introduction As you can see from the picture above, we create a board with 15 green tiles and one white tile which represent a empty slot. Our gaol is to place all the 15 green tiles back into its sorted order, namely: A B C D E F G H I J K L M N O If we click on the tile "F", the tile will slide down south. And if all the tiles are back into their sorted order, you won the game and quit the game. That's pretty much the gist of the game, later you could add other features to the game. Import modules and define contatnts impor...
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