In this article, we share the example of python to achieve a small game of five pieces of specific code for your reference, the details are as follows
I learned python for ten days in the summer, and then I wrote a little game of backgammon using the pygame module, and the code was shared with those who were interested.
import numpy as np import pygame import sys import traceback import copy from import * () () #Color background=(201,202,187) checkerboard=(80,80,80) button=(52,53,44) #Music play_chess_sound = ("music/play_chess.wav") play_chess_sound.set_volume(0.2) button_sound = ("music/") button_sound.set_volume(0.2) victor_sound = ("music/") victor_sound.set_volume(0.2) #Drawing the board def Draw_a_chessboard(screen): #Fill background color (background) Background=("").convert_alpha() (Background,(0,0)) #Drawing the board for i in range(21): (screen, checkerboard, (40*i+3, 3), (40*i+3, 803)) (screen, checkerboard, (3, 40*i+3), (803, 40*i+3)) #Drawing a borderline (screen, checkerboard, (3, 3), (803, 3),5) (screen, checkerboard, (3, 3), (3, 803),5) (screen, checkerboard, (803, 3), (803, 803),5) (screen, checkerboard, (3, 803), (803, 803),5) #Draw the locus (screen, checkerboard, (163, 163), 6) (screen, checkerboard, (163, 643), 6) (screen, checkerboard, (643, 163), 6) (screen, checkerboard, (643, 643), 6) (screen, checkerboard, (403, 403), 6) # Draw 'Repent', 'Restart' and 'Quit' buttons. (screen,button,[900,350,120,100],5) (screen,button,[900,500,200,100],5) (screen,button,[900,650,200,100],5) s_font=('',40) text1=s_font.render("Repent.",True,button) text2=s_font.render("Start over.",True,button) text3=s_font.render("Quit the game.",True,button) (text1,(920,370)) (text2,(920,520)) (text3,(920,670)) # Draw the pieces (horizontal coordinates, vertical coordinates, screen, piece color (1 for black, 2 for white)) def Draw_a_chessman(x,y,screen,color): if color==1: Black_chess=("Black_chess.png").convert_alpha() (Black_chess,(40*x+3-15,40*y+3-15)) if color==2: White_chess=("White_chess.png").convert_alpha() (White_chess,(40*x+3-15,40*y+3-15)) #Drawing a chessboard with pieces def Draw_a_chessboard_with_chessman(map,screen): (background) Draw_a_chessboard(screen) for i in range(24): for j in range(24): Draw_a_chessman(i+1,j+1,screen,map[i][j]) # Define a list of stored boards. The # list is 24 columns and 24 rows because the index in the win function exceeds 19. A larger #list won't affect the game much map=[] for i in range(24): ([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]) # Clear the map list def clear(): global map for i in range(24): for j in range(24): map[i][j]=0 #Judge whether you're winning def win(i, j): k = map[i][j] p=[] for a in range(20): (0) for i3 in range(i-4,i+5): for j3 in range(j-4,j+5): if (map[i3][j3] == k and i3 - i == j3 - j and i3 <= i and j3 <= j): p[0]+=1 if (map[i3][j3] == k and j3 == j and i3 <= i and j3 <= j): p[1]+=1 if (map[i3][j3] == k and i3 == i and i3 <= i and j3 <= j): p[2]+=1 if (map[i3][j3] == k and i3 - i == j3 - j and i3 >= i and j3 >= j): p[3]+=1 if (map[i3][j3] == k and j3 == j and i3 >= i and j3 >= j): p[4]+=1 if (map[i3][j3] == k and i3 == i and i3 >= i and j3 >= j): p[5]+=1 if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i and j3 >= j): p[6]+=1 if (map[i3][j3] == k and i3 - i == j - j3 and i3 >= i and j3 <= j): p[7]+=1 if (map[i3][j3] == k and j - j3 == i - i3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3): p[8]+=1 if (map[i3][j3] == k and j == j3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3): p[9]+=1 if (map[i3][j3] == k and i == i3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3): p[10]+=1 if (map[i3][j3] == k and j - j3 == i - i3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3): p[11]+=1 if (map[i3][j3] == k and j == j3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3): p[12]+=1 if (map[i3][j3] == k and i == i3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3): p[13]+=1 if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i + 1 and i3 >= i - 3 and j3 >= j - 1 and j3 <= j + 3): p[14]+=1 if (map[i3][j3] == k and i3 - i == j - j3 and i3 >= i - 1 and i3 <= i + 3 and j3 <= j + 1 and j3 >= j - 3): p[15]+=1 if (map[i3][j3] == k and j - j3 == i - i3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2): p[16]+=1 if (map[i3][j3] == k and j == j3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2): p[17]+=1 if (map[i3][j3] == k and i == i3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2): p[18]+=1 if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2): p[19]+=1 for b in range(20): if p[b]==5: return True return False # Drawing Prompter (class, screen, word size) def text(s,screen,x): # First cover the last class capacity with a rectangle # (screen,background,[850,100,1200,100]) #Define font and size s_font=('',x) #Define the class capabilities, whether or not they are anti-aliased, colors s_text=s_font.render(s,True,button) # Place the word at the specified position in the window (s_text,(880,100)) () # For control sequences t=True # Used to stop the drop after the end of the game running=True #main function def main(): # Set t, map, running to changeable global t,map,running,maps,r,h # Set the map to zero clear() # Define a list that stores all board states (for remorse) map2=(map) maps=[map2] #Define Window screen = .set_mode([1200,806]) #Define the window name .set_caption("Pentominoes.") # Draw boards, cues and buttons in windows Draw_a_chessboard(screen) () clock=() while True: # Only when running is true can you make a move, this is mainly used to prevent making another move after the game is over. if running: if t: color=1 text('Black discs fall.',screen,54) else: color=2 text('White discs fall.',screen,54) for event in (): #Click x to close the window if ==: () () #Clicking on a class in the window completes the corresponding command. elif == MOUSEBUTTONDOWN: if == 1: x,y=[0],[1] for i in range(19): for j in range(19): # Click on the corresponding position on the board if i*40+3+20<x<i*40+3+60 and j*40+3+20<y<j*40+3+60 and not map[i][j] and running: # Drop a piece of the appropriate color on the corresponding position of the board. Draw_a_chessman(i+1,j+1,screen,color) #Play sound effects play_chess_sound.play(0) # Record the drop position in the map map[i][j]=color #Store maps into maps map3=(map) (map3) #Judging whether there is a pentomino line after a drop if win(i,j): if t: text('Black wins, please play again',screen,30) else: text('White wins, please play again',screen,30) #Play sound effects victor_sound.play(0) # Stopping further moves on the board running=False () t=not t #If you click 'Restart' if 900<x<1100 and 500<y<600: # Unblock running=True #Play sound effects button_sound.play(0) # Starting over main() #Click 'Exit Game' to exit the game elif 900<x<1100 and 650<y<750: #Play sound effects button_sound.play(0) () () #Click on 'Repent' # elif 900<x<1020 and 350<y<450 and len(maps)!=1: #Play sound effects button_sound.play(0) # Delete the last element in the maps del maps[len(maps)-1] # And then copy the last element to map map=(maps[len(maps)-1]) #Switching order t=not t # Show the map to the world Draw_a_chessboard_with_chessman(map,screen) #Repent to finish, preventing another repentance. x,y=0,0 (60) if __name__ == "__main__": try: main() except SystemExit: pass except: traceback.print_exc() () input()
The realization is shown below:
For more great articles on python games check out the following topics:
Collection of python tetris games
Summary of classic python mini-games
python wechat jump game collection
This is the whole content of this article.