[Game learning essay 01] challenge task 01

Today is February 6, 2020. Time flies so fast that when I write about time, I will subconsciously write 2019

It seems that the situation of pneumonia in the whole country has further heated up, so that the community where my family is located has issued an order to block the notice, so the travel is no longer as free as before. Anyway, cheer on the anti pneumonia doctors and relevant staff who are fighting in the front line.

To put it bluntly, today I have completed a small challenge in the process of python learning. Generally speaking, I haven't encountered any special problems. It belongs to procedural programming and hands-on training.

 

The code is as follows:

 1 #Title: the receipt rectangle case is the only one that moves around the screen. Modify the program so that the rectangle will change color whenever it touches the screen border.
 2 
 3 import pygame,sys,math,time
 4 from pygame.locals import *
 5 
 6 #pygame Initialization
 7 pygame.init()
 8 
 9 #Set title
10 pygame.display.set_caption("Game_One")
11 
12 #Set window size
13 screen=pygame.display.set_mode((600,400))
14 
15 #Defining variables
16 color=0  #Color variable
17 sign=0  #Mobile logo
18 p_x=0
19 p_y=0
20 x=0
21 y=0
22 
23 #Game cycle
24 while True :
25     color=255,255,255
26     sign=1
27     for event in pygame.event.get() :
28         if event.type==QUIT :
29             pygame.quit()
30             sys.exit()
31 
32     if p_x==550 :
33         sign=2
34         color=0,255,255
35     if p_y==350 :
36         sign=3
37         color=0,0,255
38     if p_x==0 :
39         sign=4
40         color=255,0,255
41     if p_x==0 and p_y==0 :
42         sign=1
43         color=255,255,255
44 
45     if sign==1 :
46         print("1...")  #For testing, it can be deleted.
47         p_x+=1
48         p_y=0
49     elif sign==2 :
50         print("2...")
51         p_x=550
52         p_y+=1
53     elif sign==3 :
54         print("3...")
55         p_x-=1
56         p_y=350
57     elif sign==4 :
58         print("4...")
59         p_x=0
60         p_y-=1
61 
62     screen.fill((0,0,0))
63     pygame.draw.rect(screen,color,(p_x,p_y,50,50),0)
64     time.sleep(0.005)
65     pygame.display.update()

 

Effect display:

Tags: Python Programming Mobile

Posted on Fri, 07 Feb 2020 11:59:56 -0500 by linda_v