Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pygame tetris game tutorial

# GREPPER DIDNT ALLOW ME TO ADD THE COMPLETE CODE. CLICK THE LINK FOR A FULL TUTORIAL

import pygame
import random

pygame.font.init()

# GLOBALS VARS
s_width = 800
s_height = 700
play_width = 300  # meaning 300 // 10 = 30 width per block
play_height = 600  # meaning 600 // 20 = 30 height per block
block_size = 30

top_left_x = (s_width - play_width) // 2
top_left_y = s_height - play_height

shapes = [S, Z, I, O, J, L, T]
shape_colors = [(0, 255, 0), (255, 0, 0), (0, 255, 255), (255, 255, 0), (255, 165, 0), (0, 0, 255), (128, 0, 128)]
# index 0 - 6 represent shape

.
.
.
.

def main(win):  # *
    last_score = max_score()
    locked_positions = {}
    grid = create_grid(locked_positions)

    change_piece = False
    run = True
    current_piece = get_shape()
    next_piece = get_shape()
    clock = pygame.time.Clock()
    fall_time = 0
    fall_speed = 0.27
    level_time = 0
    score = 0

    while run:
        grid = create_grid(locked_positions)
        fall_time += clock.get_rawtime()
        level_time += clock.get_rawtime()
        clock.tick()

        if level_time/1000 > 5:
            level_time = 0
            if level_time > 0.12:
                level_time -= 0.005

        if fall_time/1000 > fall_speed:
            fall_time = 0
            current_piece.y += 1
            if not(valid_space(current_piece, grid)) and current_piece.y > 0:
                current_piece.y -= 1
                change_piece = True

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.display.quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    current_piece.x -= 1
                    if not(valid_space(current_piece, grid)):
                        current_piece.x += 1
                if event.key == pygame.K_RIGHT:
                    current_piece.x += 1
                    if not(valid_space(current_piece, grid)):
                        current_piece.x -= 1
                if event.key == pygame.K_DOWN:
                    current_piece.y += 1
                    if not(valid_space(current_piece, grid)):
                        current_piece.y -= 1
                if event.key == pygame.K_UP:
                    current_piece.rotation += 1
                    if not(valid_space(current_piece, grid)):
                        current_piece.rotation -= 1

        shape_pos = convert_shape_format(current_piece)

        for i in range(len(shape_pos)):
            x, y = shape_pos[i]
            if y > -1:
                grid[y][x] = current_piece.color

        if change_piece:
            for pos in shape_pos:
                p = (pos[0], pos[1])
                locked_positions[p] = current_piece.color
            current_piece = next_piece
            next_piece = get_shape()
            change_piece = False
            score += clear_rows(grid, locked_positions) * 10

        draw_window(win, grid, score, last_score)
        draw_next_shape(next_piece, win)
        pygame.display.update()

        if check_lost(locked_positions):
            draw_text_middle(win, "YOU LOST!", 80, (255,255,255))
            pygame.display.update()
            pygame.time.delay(1500)
            run = False
            update_score(score)


def main_menu(win):
    run = True
    while run:
        win.fill((0,0,0))
        draw_text_middle(win, 'Press Any Key To Play', 60, (255,255,255))
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.KEYDOWN:
                main(win)

    pygame.display.quit()


win = pygame.display.set_mode((s_width, s_height))
pygame.display.set_caption('Tetris')
main_menu(win)
Comment

PREVIOUS NEXT
Code Example
Python :: how to plotting points on matplotlib 
Python :: python opencv create new image 
Python :: how plot graph by using group by function in python 
Python :: convert file to base64 python 
Python :: scrape with beautiful soup 
Python :: numpy slice array into chunks 
Python :: sklearn adjusted r2 
Python :: json post python with headers 
Python :: python detect color on screen 
Python :: triangle pattern in python 
Python :: how to set interval in python 
Python :: zermelo python 
Python :: add element to heap python 
Python :: numpy.datetime64 to datetime 
Python :: converting bool to 1 if it has true and if it is false print 1 
Python :: pandas rename column name 
Python :: tqdm in python 
Python :: python sort dataframe by one column 
Python :: change tick labelsize matplotlib 
Python :: emacs region indent python 
Python :: python date from yy/mm/dd to yy-mm-dd 
Python :: euclidean distance python 
Python :: remove consecutive duplicates python 
Python :: dict godot 
Python :: how to clear a text file in python 
Python :: python command not found 
Python :: python named tuple 
Python :: how to stop running code in python 
Python :: convert bytes to numpy array python 
Python :: python check if string is number 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =