"Python Game Programming Tour" Eighth---pygame game development of common data structures

Source: Internet
Author: User



This series of blogs introduces the development of mini-games in the Python+pygame library. There is the wrong point of writing and hope you haihan.






Last blog we studied together. Pygame Conflict Detection technology: http://www.cnblogs.com/msxh/p/5027688.html



This time we will learn some of the data structures commonly used in Pygame games: data, lists, tuples, queues, stacks.






First, arrays and lists



The array can be understood as a simplified list. A list of sprites like the pygame.sprite.Group we used before. The elements of a list are mutable, and they have many ways of adding, deleting, searching, sorting, and so on.



1, one-dimensional list



You can define all the elements at once when you create the list, or you can add them to the list later:


name = ["zhangsan","lisi","wangwu"] print name
[‘zhangsan‘, ‘lisi‘, ‘wangwu‘]


modifying elements



You can get the data for any element in the list by index number, or you can modify the element value by referencing the index number (the index number starts at 0):


name = ["zhangsan","lisi","wangwu"]
print name

print name[0]
name[0] = "zhaoliu"

print name
---------------------------------------------------
[‘zhangsan‘, ‘lisi‘, ‘wangwu‘]
zhangsan
[‘zhaoliu‘, ‘lisi‘, ‘wangwu‘]




To add an element:



You can use the Append method to add an element to the list:


name = ["zhangsan","lisi","wangwu"]
print name

name.append("zhaoliu")
print name
-----------------------------------------------------


[' Zhangsan ', ' Lisi ', ' Wangwu ']
[' Zhangsan ', ' Lisi ', ' Wangwu ', ' Zhaoliu ']



You can also insert an element into the middle of the list by using the Insert (Index,value) method, which represents the index parameter, and value represents the values.


name = ["zhangsan","lisi","wangwu"]
print name

name.insert(0,"zhaoliu")
print name
name.insert(1,"liuqi")
print name
-------------------------------------------------
[‘zhangsan‘, ‘lisi‘, ‘wangwu‘]
[‘zhaoliu‘, ‘zhangsan‘, ‘lisi‘, ‘wangwu‘]
[‘zhaoliu‘, ‘liuqi‘, ‘zhangsan‘, ‘lisi‘, ‘wangwu‘]


Statistics on elements:



If you have duplicate elements in a list, you can count them by using count (), which is actually counting the occurrences of an element in a list:


num =[]

num.append(10)
num.append(10)
num.append(90)
num.append(79)
num.append(10) print num.count(10) ---------------------------
3


Search for elements:



You can use the index () method to search for where an element first appears in the list:


num =[90,10,89,78,30,40,90,10] print (num.index(10)) -----------------------------------------
1


To delete an element:



Use the Remove () method to remove an element from the list.


num =[90,10,89,78,30,40,90,10] print (num)
num.remove(10) print (num) -------------------------------------------- [90, 10, 89, 78, 30, 40, 90, 10]
[90, 89, 78, 30, 40, 90, 10]


List element inversion:



Use the reverse () method to flip the entire list:


num =[90,10,89,78,30,40,90,10] print (num)
num.reverse() print (num) ------------------------------------------ [90, 10, 89, 78, 30, 40, 90, 10]
[10, 90, 40, 30, 78, 89, 10, 90]


List sort:



You can use the Sort method to sort the elements in a list (the default is ascending):


num =[9,11,35,2,49,12,8,5,99] print (num)
num.sort() print (num) --------------------------------------- [9, 11, 35, 2, 49, 12, 8, 5, 99]
[2, 5, 8, 9, 11, 12, 35, 49, 99]





2. Stacked List



The stack uses a last-in-first-out method to manage elements. The Pop method pops up the element at the top of the stack (the last item in the list), and append is used to push an element into the stack:


stack = [] for i in range(10):
    stack.append(i) print (stack)
stack.append(10) print (stack)
j = stack.pop()
k = stack.pop() print (j,k) print (stack) ------------------------------------------ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
(10, 9)
[0, 1, 2, 3, 4, 5, 6, 7, 8]


3. Queue-style list:



The queue uses the FIFO mode management element, and Python has a module named queue that implements the queuing function, in order to illustrate that we use a list here instead of a queue:


queue = [] for i in range(10):
    queue.append(i) print (queue)
queue.append(89)
queue.append(70) print (queue)
n = queue[0]
queue.remove(n) print (queue) ---------------------------------- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 89, 70]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 89, 70]





Two, the meta-group



A tuple is a data type similar to a list, but the elements of a tuple are read-only and cannot be changed. Once initialized, the elements inside can no longer be changed. The advantage of a tuple's comparison list is that the tuple is faster.



1. Initialize a tuple



The process of creating a tuple is called Packaging:


tup = (1,2,3,4,5)
print tup
---------------------
(1, 2, 3, 4, 5)


If you try to modify the elements in the tuple at this point, the program will error.


 
 
Traceback (most recent call last):
  File "C:/Users/Administrator/Desktop/1.py", line 4, in <module>
    tup[0]=20
TypeError: ‘tuple‘ object does not support item assignment


2. Unpacking a tuple



The process of reading an element is called unpacking.



3. Searching for elements



You can use the index method to return the position where an element first appears. You can also use the in () method to search for whether an element is in a tuple:


tup = (1,2,3,4,5) print (10 in tup) print (2 in tup) -------------------- False
True





These are some of the data structures commonly used in the development of Pygame games, and it is easy to know what Python has been exposed to.



In the next blog we will work together to make a cool running game: "Aw big meow run." As follows:






"Python Game Programming Tour" Eighth---pygame game development of common data structures


Related Article

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.