Get started with Python game programming, let you become a computer master in seconds!

foreword

Getting to know pygame: pie games
The pygame library makes it possible to draw graphics, get user input, perform animations
, and use timers to keep games running at a steady frame rate.
Use the pygame library;
print text in a certain font;
use loops to repeat actions;
draw circles, rectangles, lines, and house types;
create pie games;

[----Help Python learning, all the following learning materials are free at the end of the article! ----】

Where to get the pygame library: http://www.pygame.org/download.shtml
I am using Python2.7 and pygame1.9
The environment used in the book is Python3.2 and pygame1.9
is not installed in the Python3 environment now Using the pip tool causes the environment to be inconsistent

The initialization of the pygame library works:
import pygame
from pygame.locals import *
pygame.init()

Create a screen with a size of (600,500)
screen=pygame.display.set_mode((600,500))
screen is assigned the value <Surface(600x500x32 SW)> at the same time
. This is a useful value, so it is stored in the screen variable.

Print text
1. Create a font object
myfont=pygame.font.Font(None,60)
None: use the default font
60: font size
2. Create a plane that can be drawn using screen.blit() textimage
=myfont.render("Hello Python”, True, (255, 255, 255))
render requires three parameters, the string to be displayed, whether to anti-alias True/False, color
3, and hand over the textimage to screen.blit() for drawing
screen.blit(textimage,( 100,100))
screen.blit() requires two parameters, the object to be drawn and its (upper left vertex) coordinates

background fill
screen.fill((0,0,0))
screen.fill() need to give background color

Refresh display
screen.display.update()
is generally used with while loop

while loop
Event processing and continuous screen refresh can be performed through the while loop
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()
screen.display.update( )

Draw a circle
pygame.draw.circle(screen,color,position,radius,width)
color (0,0,0) given color
radius circle radius
position (0,0) given circle center coordinates
width line width

Draw a rectangle
pygame.draw.rect(screen,color,position,width)
position (pos_x,pos_y,100,100) given the coordinates, length and width of the upper left corner vertex

Draw the line
pygame.draw.line(screen,color,(0,0),(100,100),width)
(0,0)(100,100) is responsible for the two endpoints of a given line segment

Draw an arc
start_angle=math.radians(0)
end_angle=math.radians(90)
position=x-radius,y-radius,radius*2,radius*2
#x,y represent the coordinates of the center of the circle where the arc is located, radius means the radius
pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
start_angle start angle pointing to the right side of the radius starts to rotate counterclockwise is 0 to 360
end_angle end angle

Two examples worth learning
1. Draw a moving rectangle
#!/usr/bin/python

import sys
import random
from random import randint
import pygame
from pygame import *
pygame.init()

screen=pygame.display.set_mode((600,500))
pygame.display.set_caption(“Drawing Rectangles”)
pos_x=300
pos_y=250
vel_x=2
vel_y=1
color=100,100,100
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()

screen.fill((0,0,200))  
  
pos\_x +=vel\_x  
pos\_y +=vel\_y  
if pos\_x>500 or pos\_x<0:  
    vel\_x=-vel\_x  
    rand1=randint(0,255)  
    rand2=randint(0,255)  
    rand3=randint(0,255)  
    color=rand1,rand2,rand3  
if pos\_y>400 or pos\_y<0:  
    vel\_y=-vel\_y  
    rand1=randint(0,255)  
    rand2=randint(0,255)  
    rand3=randint(0,255)  
    color=rand1,rand2,rand3  
width=0  
pos=pos\_x,pos\_y,100,100  
pygame.draw.rect(screen,color,pos,width)  

pygame.display.update()  

2. pie game
#!/usr/bin/python

#init
import sys
import math
import pygame
from pygame.locals import *
pygame.init()

screen=pygame.display.set_mode((600,500))
pygame.display.set_caption(“The Pie Game-Press 1,2,3,4”)
myfont=pygame.font.Font(None,60)

color=200,80,60
width=4
x=300
y=250
radius=200
position=x-radius,y-radius,radius*2,radius*2

piece1=False
piece2=False
piece3=False
piece4=False

while True:
for event in pygame.event.get():
if event.typeQUIT:
sys.exit()
elif event.type
KEYUP:
if event.keypygame.K_ESCAPE:
sys.exit()
elif event.key
pygame.K_1:
piece1=True
elif event.keypygame.K_2:
piece2=True
elif event.key
pygame.K_3:
piece3=True
elif event.key==pygame.K_4:
piece4=True

screen.fill((0,0,200))  

#draw the four numbers  
textimage1=myfont.render("1",True,color)  
screen.blit(textimage1,(x+radius/2-20,y-radius/2))      
textimage2=myfont.render("2",True,color)  
screen.blit(textimage2,(x-radius/2,y-radius/2))  
textimage3=myfont.render("3",True,color)  
screen.blit(textimage3,(x-radius/2,y+radius/2-20))  
textimage4=myfont.render("4",True,color)  
screen.blit(textimage4,(x+radius/2-20,y+radius/2-20))  

#draw arc,line  
if piece1:  
    start\_angle=math.radians(0)  
    end\_angle=math.radians(90)  
    pygame.draw.arc(screen,color,position,start\_angle,end\_angle,width)  
    pygame.draw.line(screen,color,(x,y),(x+radius,y),width)  
    pygame.draw.line(screen,color,(x,y),(x,y-radius),width)  

if piece2:      
    start\_angle=math.radians(90)  
    end\_angle=math.radians(180)  
    pygame.draw.arc(screen,color,position,start\_angle,end\_angle,width)  
    pygame.draw.line(screen,color,(x,y),(x,y-radius),width)  
    pygame.draw.line(screen,color,(x,y),(x-radius,y),width)  

if piece3:  
    start\_angle=math.radians(180)  
    end\_angle=math.radians(270)  
    pygame.draw.arc(screen,color,position,start\_angle,end\_angle,width)  
    pygame.draw.line(screen,color,(x,y),(x-radius,y),width)  
    pygame.draw.line(screen,color,(x,y),(x,y+radius),width)  

if piece4:  
    start\_angle=math.radians(270)  
    end\_angle=math.radians(360)  
    pygame.draw.arc(screen,color,position,start\_angle,end\_angle,width)  
    pygame.draw.line(screen,color,(x,y),(x,y+radius),width)  
    pygame.draw.line(screen,color,(x,y),(x+radius,y),width)  
      
#if success,display green  
if piece1 and piece2 and piece3 and piece4:  
    color=0,255,0  

pygame.display.update()  

Challenge
1. Draw an ellipse
#!/usr/bin/python

import sys
import pygame
from pygame.locals import *
pygame.init()

screen=pygame.display.set_mode((600,500))
pygame.display.set_caption(“Drawing Ellipse”)

while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()

    screen.fill((0,255,0))  

    color=255,0,255  
    position=100,100,400,300  
    width=8  
    pygame.draw.ellipse(screen,color,position,width)  

    pygame.display.update()  

This topic is to let you know about the related use of the pygame.draw.ellipse() function.
This function is used in a very similar way to the pygame.draw.rect() function.

2. Randomly draw 1000 lines
#!/usr/bin/python
import random
from random import randint
import sys
import pygame
from pygame import *
pygame.init()

screen=pygame.display.set_mode((800,600))
pygame.display.set_caption(“Drawing Line”)

screen.fill((0,80,0))
color=100,255,200
width=2
for i in range(1,1001):
pygame.draw.line(screen,color,(randint(0,800),randint(0,600)),(randint(0,800),randint(0,600)),width)

while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()

    pygame.display.update()  

Through this topic, I understand that if drawing graphics and refreshing the display are both in the loop, the while True loop will draw
graphics and refresh the display every time.
Call the randint() function in the pygame module.
However, if the graphics are drawn outside the while True loop, the graphics will remain unchanged after the graphics are drawn. Refreshing displays a
graph that has already been drawn. If you are interested in Python, you can join our learning exchange group: 649, 825, 285, and get a set of learning materials and video courses for free~

3. Modify the rectangle program so that when the rectangle touches the border of the screen, the rectangle will change color

#!/usr/bin/python

import sys
import random
from random import randint
import pygame
from pygame import *
pygame.init()

screen=pygame.display.set_mode((600,500))
pygame.display.set_caption(“Drawing Rectangles”)
pos_x=300
pos_y=250
vel_x=2
vel_y=1
rand1=randint(0,255)
rand2=randint(0,255)
rand3=randint(0,255)
color=rand1,rand2,rand3
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()

screen.fill((0,0,200))  
  
pos\_x +=vel\_x  
pos\_y +=vel\_y  
if pos\_x>500 or pos\_x<0:  
    vel\_x=-vel\_x  
    rand1=randint(0,255)  
    rand2=randint(0,255)  
    rand3=randint(0,255)  
    color=rand1,rand2,rand3  
if pos\_y>400 or pos\_y<0:  
    vel\_y=-vel\_y  
    rand1=randint(0,255)  
    rand2=randint(0,255)  
    rand3=randint(0,255)  
    color=rand1,rand2,rand3  
width=0  
pos=pos\_x,pos\_y,100,100  
pygame.draw.rect(screen,color,pos,width)  

pygame.display.update()  

The random module is needed here. Every time the border of the screen is touched, not only the movement direction of the rectangle is changed, but also the color of the rectangle is changed using a random number.
You can also set the color to a fixed value first, and you can save three lines of code.

Finally, I will introduce a complete python learning route, from entry to advanced, including mind maps, classic books, and supporting videos, to help those who want to learn python and data analysis!

1. Introduction to Python

The following content is the basic knowledge necessary for all application directions of Python. If you want to do crawlers, data analysis or artificial intelligence, you must learn them first. Anything tall is built on primitive foundations. With a solid foundation, the road ahead will be more stable.

Include:

Computer Basics

insert image description here

python basics

insert image description here

Python introductory video 600 episodes:

Watching the zero-based learning video is the fastest and most effective way to learn. Following the teacher's ideas in the video, it is still very easy to get started from the basics to the in-depth.

2. Python crawler

As a popular direction, reptiles are a good choice whether it is a part-time job or as an auxiliary skill to improve work efficiency.

Relevant content can be collected through crawler technology, analyzed and deleted to get the information we really need.

This information collection, analysis and integration work can be applied in a wide range of fields. Whether it is life services, travel, financial investment, product market demand of various manufacturing industries, etc., crawler technology can be used to obtain more accurate and effective information. use.

insert image description here

Python crawler video material

insert image description here

3. Data Analysis

According to the report "Digital Transformation of China's Economy: Talents and Employment" released by the School of Economics and Management of Tsinghua University, the gap in data analysis talents is expected to reach 2.3 million in 2025.

With such a big talent gap, data analysis is like a vast blue ocean! A starting salary of 10K is really commonplace.

insert image description here

4. Database and ETL data warehouse

Enterprises need to regularly transfer cold data from the business database and store it in a warehouse dedicated to storing historical data. Each department can provide unified data services based on its own business characteristics. This warehouse is a data warehouse.

The traditional data warehouse integration processing architecture is ETL, using the capabilities of the ETL platform, E = extract data from the source database, L = clean the data (data that does not conform to the rules), transform (different dimension and different granularity of the table according to business needs) calculation of different business rules), T = load the processed tables to the data warehouse incrementally, in full, and at different times.

insert image description here

5. Machine Learning

Machine learning is to learn part of the computer data, and then predict and judge other data.

At its core, machine learning is "using algorithms to parse data, learn from it, and then make decisions or predictions about new data." That is to say, a computer uses the obtained data to obtain a certain model, and then uses this model to make predictions. This process is somewhat similar to the human learning process. For example, people can predict new problems after obtaining certain experience.

insert image description here

Machine Learning Materials:

insert image description here

6. Advanced Python

From basic grammatical content, to a lot of in-depth advanced knowledge points, to understand programming language design, after learning here, you basically understand all the knowledge points from python entry to advanced.

insert image description here

At this point, you can basically meet the employment requirements of the company. If you still don’t know where to find interview materials and resume templates, I have also compiled a copy for you. It can really be said to be a systematic learning route for nanny and .

insert image description here
But learning programming is not achieved overnight, but requires long-term persistence and training. In organizing this learning route, I hope to make progress together with everyone, and I can review some technical points myself. Whether you are a novice in programming or an experienced programmer who needs to be advanced, I believe that everyone can gain something from it.

It can be achieved overnight, but requires long-term persistence and training. In organizing this learning route, I hope to make progress together with everyone, and I can review some technical points myself. Whether you are a novice in programming or an experienced programmer who needs to be advanced, I believe that everyone can gain something from it.

Data collection

This full version of the full set of Python learning materials has been uploaded to the official CSDN. If you need it, you can click the CSDN official certification WeChat card below to get it for free ↓↓↓ [Guaranteed 100% free]

insert image description here

Good article recommended

Understand the prospect of python: https://blog.csdn.net/SpringJavaMyBatis/article/details/127194835

Learn about python's part-time sideline: https://blog.csdn.net/SpringJavaMyBatis/article/details/127196603

Guess you like

Origin blog.csdn.net/weixin_49892805/article/details/131268567