Python is an interpreted, object-oriented, dynamic data type advanced programming language. It has a rich and powerful library that can easily connect various modules made in other languages (especially C/C++). In the past two years, as people pay more and more attention to artificial intelligence, everyone\'s enthusiasm for learning Python has also increased. Python ranks first in the 2017 programming language rankings released by IEEE. This book introduces the basics of Python programming, and uses the game \"Minecraft\" as a carrier to interact with players or blocks in the game through Python programming. It has successively realized the \"Sword Ball\" game, Gobang game, and pixel image scanner. Finally, it also realizes the function of controlling external devices such as Arduino through the game. I hope that everyone can easily enter the world of Python during the game, and finally cross the gap between software and hardware, and try automatic control initially. This book provides a new experience of learning programming while playing games, and is suitable for readers who are interested in Python programming. Games are no longer just for playing. You will experience the roles of players, designers, and programmers at the same time. 1Python Basics 1 1.1Python History 1 1.2Python Development 1 1.3Python Advantages and Disadvantages 2 1.3.1Python Advantages 2 1.3.2Python Disadvantages 2 1.4Python Application Fields 3 1.5Python Installation and Use 3 1.5.1Python Installation 3 1.5.2Python Use 5 1.5.3Editor 7 1.6Keywords 9 1.7Numbers 10 1.8Variables 10 1.9The Basic Structure of Programs 11 1.9.1for loop 12 1.9.2if selection structure 13 1.9.3Comparison 14 1.9.4Logical Operations 14 1.10Dice Rolling 16 1.10.1Random Numbers 16 1.10.2Repeated Dice Rolling 16 1.10.3Rolling Two Dice 17 1.10.4Size Judgment 18 1.11While 20 2Strings, Lists and Dictionaries 22 2.1Strings 22 2.1.1 Definition of string 22 2.1.2 Methods of string 22 2.2 List 24 2.2.1 Definition of list 24 2.2.2 Methods of list 25 2.3 Function 26 2.4 Guessing word game 27 2.4.1 Game rules 27 2.4.2 Creating a word library 27 2.4.3 Game structure 28 2.4.4 Perfecting function 29 2.5 Dictionary 34 2.6 Tuple 35 2.6.1 Definition of tuple 35 2.6.2 Multiple assignment 35 2.6.3 Multiple return values 36 2.7 Exception 36 2.8 Function summary 37 2.8.1 Number 37 2.8.2 String 38 2.8.3 List 39 2.8.4 Dictionary 40 2.8.5 Type conversion 40 3 Class library and method 42 3.1 Library 42 3.1.1 Using random library 42 3.1.2 Using the Python Standard Library 43 3.2 Object Orientation 44 3.3 Defining Classes 44 3.4 Inheritance 46 3.5 Files 48 3.5.1 Reading Files 48 3.5.2 Reading Large Files 50 3.5.3 Writing Files 51 3.5.4 File System Operations 51 3.6 Erosion 52 3.7 Network 52 4 Minecraft 54 4.1 What is Minecraft 54 4.2 Getting Started 55 4.3 Welcome to Minecraft 55 4.4 Position Coordinates 60 4.5 Player Position 60 4.5.1 Getting the Player\'s Position 60 4.5.2 Displaying the Player\'s Position 61 4.5.3 Setting the Player\'s Position 62 4.6 Catapult Zone 62 5 Sword and Ball Game 64 5.1 Building a Court 64 5.1.1 Clearing Space 64 5.1.2 Laying the Court 65 5.1.3 Building the Goal 67 5.1.4 Court Function 67 5.2 Hit Event Processing 68 5.2.1 Get Hit Event 68 5.2.2 Confirm the Face of the Block 70 5.3 Ball Movement 71 5.3.1 Hit the Different Faces of the Block 71 5.3.2 Procedure for Moving the Ball 73 5.4 Abnormal Situation 74 5.4.1 More Balls 74 5.4.2 No Ball 75 5.5 Out of Bounds and Goals 77 5.5.1 Out of Bounds 77 5.5.2 Goals 78 6 Sword Ball Advanced 81 6.1 Special Rules 81 6.1.1 Introduction to the Rules 81 6.1.2 Moving Center Line 81 6.1.3 Maximum Offset 83 6.2 Display Numbers 83 6.3 Score on the Goal 86 6.3.1 Score on the East Side 86 6.3.2 Score on the West Side 94 6.4 CSV File 99 6.4.1 What is a CSV file? 99 6.4.2 Create a new CSV file 100 6.4.3 Read an external file 100 6.4.4 Optimize the code 102 6.5 Easter egg: TNT is coming 106 6.6 Display time 108 6.6.1 Get system time 108 6.6.2 Display format 109 6.6.3 Function implementation 109 7 Gobang 115 7.1 Draw a chessboard 115 7.1.1 Two-dimensional array 115 7.1.2 Code implementation 116 7.2 Move 117 7.2.1 Get a hit event 118 7.2.2 Change the color of the block 118 7.2.3 Change the array 118 7.3 Five pieces connected 118 7.3.1 The effect of the move 118 7.3.2 The problem of repeated moves 119 7.3.3 Winning judgment 121 7.4 Restart 128 7.4.1 Tips after the end 128 7.4.2 Refresh the chessboard 129 7.4.3 Repeated refresh problem 130 8 Image scanner 131 8.1 Pixel painting 131 8.2 Project introduction 132 8.3 Operation menu 133 8.3.1 Get input information 133 8.3.2 Make menu 135 8.3.3 Chinese input method 138 8.3.4 Scanner menu 138 8.4 Scan operation 142 8.5 Copy operation 146 8.6 Demonstration operation 148 8.6.1 Original content creation 148 8.6.2 Scan 148 8.6.3 Copy 152 8.7 Complete code 154 9 Hardware control 160 9.1 RaspberryPi hardware interface 160 9.1.1 Connect GPIO 160 9.1.2 Digital output 161 9.1.3 Analog output 163 9.2 Serial communication 164 9.2.1 What is Ardui64 9.2.2 Install Ardui64 on RaspberryPi 5 9.2.3 Arduino and Python communication 165 9.3 Bit switch 166 9.3.1 Function description 166 9.3.2 Preparation 167 9.3.3 Function implementation 167 Appendix Block material name comparison table 169
You Might Like
Recommended ContentMore
Open source project More
Popular Components
Searched by Users
Just Take a LookMore
Trending Downloads
Trending ArticlesMore