Simple puzzle game instance implemented by Python and pyGame

Source: Internet
Author: User

Simple puzzle game instance implemented by Python and pyGame

This example describes a simple jigsaw puzzle game implemented by Python and pyGame. Share it with you for your reference. The specific implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

Import pygame, sys, random

From pygame. locals import *

# Constants

Required wwidth = 500

Required wheight = 500

BACKGROUNDCOLOR = (255,255,255)

BLUE = (0, 0,255)

BLACK = (0, 0, 0)

FPS = 40

VHNUMS = 3

CELLNUMS = VHNUMS * VHNUMS

MAXRANDTIME = 100

# Exit

Def terminate ():

Pygame. quit ()

Sys. exit ()

# Randomly generate game disks

Def newGameBoard ():

Board = []

For I in range (CELLNUMS ):

Board. append (I)

BlackCell = CELLNUMS-1

Board [blackCell] =-1

For I in range (MAXRANDTIME ):

Direction = random. randint (0, 3)

If (direction = 0 ):

BlackCell = moveLeft (board, blackCell)

Elif (direction = 1 ):

BlackCell = moveRight (board, blackCell)

Elif (direction = 2 ):

BlackCell = moveUp (board, blackCell)

Elif (direction = 3 ):

BlackCell = moveDown (board, blackCell)

Return board, blackCell

# If the blank image block is not at the leftmost, move the block on the left of the blank block to the position of the blank block.

Def moveRight (board, blackCell ):

If blackCell % VHNUMS = 0:

Return blackCell

Board [blackCell-1], board [blackCell] = board [blackCell], board [blackCell-1]

Return blackCell-1

# If the blank image block is not on the rightmost side, move the block on the right of the blank block to the position of the blank block.

Def moveLeft (board, blackCell ):

If blackCell % VHNUMS = VHNUMS-1:

Return blackCell

Board [blackCell + 1], board [blackCell] = board [blackCell], board [blackCell + 1]

Return blackCell + 1

# If the blank image block is not at the top, move the block above the blank block to the position of the blank block.

Def moveDown (board, blackCell ):

If blackCell <VHNUMS:

Return blackCell

Board [blackCell-VHNUMS], board [blackCell] = board [blackCell], board [blackCell-VHNUMS]

Return blackCell-VHNUMS

# If the blank image block is not at the bottom, move the block below the blank block to the position of the blank block.

Def moveUp (board, blackCell ):

If blackCell> = CELLNUMS-VHNUMS:

Return blackCell

Board [blackCell + VHNUMS], board [blackCell] = board [blackCell], board [blackCell + VHNUMS]

Return blackCell + VHNUMS

# Completed or not

Def isFinished (board, blackCell ):

For I in range (CELLNUMS-1 ):

If board [I]! = I:

Return False

Return True

# Initialization

Pygame. init ()

MainClock = pygame. time. Clock ()

# Loading Images

GameImage = pygame.image.load('pic.bmp ')

GameRect = gameImage. get_rect ()

# Setting window

WindowSurface = pygame. display. set_mode (gameRect. width, gameRect. height ))

Pygame. display. set_caption ('jigsaw Puzzle ')

CellWidth = int (gameRect. width/VHNUMS)

CellHeight = int (gameRect. height/VHNUMS)

Finish = False

GameBoard, blackCell = newGameBoard ()

# Main game loop

While True:

For event in pygame. event. get ():

If event. type = QUIT:

Terminate ()

If finish:

Continue

If event. type = KEYDOWN:

If event. key = K_LEFT or event. key = ord ('A '):

BlackCell = moveLeft (gameBoard, blackCell)

If event. key = K_RIGHT or event. key = ord ('D '):

BlackCell = moveRight (gameBoard, blackCell)

If event. key = K_UP or event. key = ord ('W '):

BlackCell = moveUp (gameBoard, blackCell)

If event. key = K_DOWN or event. key = ord ('s '):

BlackCell = moveDown (gameBoard, blackCell)

If event. type = MOUSEBUTTONDOWN and event. button = 1:

X, y = pygame. mouse. get_pos ()

Col = int (x/cellWidth)

Row = int (y/cellHeight)

Index = col + row * VHNUMS

If (index = blackCell-1 or index = blackCell + 1 or index = blackCell-VHNUMS or index = blackCell + VHNUMS ):

GameBoard [blackCell], gameBoard [index] = gameBoard [index], gameBoard [blackCell]

BlackCell = index

If (isFinished (gameBoard, blackCell )):

GameBoard [blackCell] = CELLNUMS-1

Finish = True

WindowSurface. fill (BACKGROUNDCOLOR)

For I in range (CELLNUMS ):

RowDst = int (I/VHNUMS)

ColDst = int (I % VHNUMS)

RectDst = pygame. Rect (colDst * cellWidth, rowDst * cellHeight, cellWidth, cellHeight)

If gameBoard [I] =-1:

Continue

RowArea = int (gameBoard [I]/VHNUMS)

ColArea = int (gameBoard [I] % VHNUMS)

RectArea = pygame. Rect (colArea * cellWidth, rowArea * cellHeight, cellWidth, cellHeight)

WindowSurface. bsurface (gameImage, rectDst, rectArea)

For I in range (VHNUMS + 1 ):

Pygame. draw. line (windowSurface, BLACK, (I * cellWidth, 0), (I * cellWidth, gameRect. height ))

For I in range (VHNUMS + 1 ):

Pygame. draw. line (windowSurface, BLACK, (0, I * cellHeight), (gameRect. width, I * cellHeight ))

Pygame. display. update ()

MainClock. tick (FPS)

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.