Posts

Showing posts with the label Pygame

How to write a snake game with python and pygame (2020 tutorial)

Image
  # snake game import pygame , sys , random from pygame.locals import * SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480 FPS = 5 TILE_SIZE = 20 # color set up BLACK = ( 0 , 0 , 0 ) WHITE = ( 255 , 255 , 255 ) GREEN = ( 0 , 255 , 0 ) RED = ( 255 , 0 , 0 ) def draw_grid (): # draw 25 horizontal lines for i in range ( 25 ): pygame . draw . line(screen, WHITE, ( 0 ,i * TILE_SIZE), ( 639 ,i * TILE_SIZE)) # draw 33 vertical lines for j in range ( 33 ): pygame . draw . line(screen, WHITE, (j * TILE_SIZE, 0 ), (j * TILE_SIZE, 479 )) def draw_snake (snake_list): for pos in snake_list: x, y = pos rect = pygame . Rect(x * TILE_SIZE,y * TILE_SIZE,TILE_SIZE, TILE_SIZE) pygame . draw . rect(screen, GREEN, rect) def moving_snake (direction): global snake_list if direction == 'E' : for i in range ( len (snake_list) - 1

How to write a slide puzzle game with Python and Pygame (2020 tutorial)

Image
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