Course: Python Programming
Student No.: 20213422
Experiment teacher: wangzhiqiang
Test date: May 29th, 2022
Compulsory / elective course: Public Elective Course
1, Experimental requirements
Comprehensive applications of Python: crawler, data processing, visualization, machine learning, neural network, game, network security, etc.
Class representatives and team leaders collect homework (source code, video, comprehensive practice report)
Note: it is programmed on Huawei ECS server (openeuler system) and physical machine (Windows/Linux system) using VIM, PDB, IDLE, pychart and other tools.
2, Experiment content
Write the gold miner game
from PIL import Image import math import pygame import sys import mod from mod import Hook from mod import Mine, Time_countdown from mod import screen # Window settings pygame.init()#Initializer pygame.display.set_caption("Gold Miner ")#Set current window title screen_size = (800, 600)#Screen size screen = pygame.display.set_mode(screen_size)#Display screen size, initialize display window or screen backcolour = (255, 255, 255)#background color screen.fill(backcolour)#Color filling FPS = 60 fclock = pygame.time.Clock()#clock # Icon initialization hook = Hook() man = mod.Matchman(hook.rect.top - 80, hook.rect.left + hook.rect.width / 2 - 50) # Create a sprite group to allow users to click the button when the start game interface is displayed news_group = pygame.sprite.Group() # Create 3 buttons (buttons to be displayed when starting the screen) for i in range(3): news_group.add(mod.Button(i)) screens = 0 goals = [100, 150, 300]#coordinate time_limited = [20, 30, 20]#time limit # integral scores = mod.Scores(0)#Integral initialization # Play background music pygame.mixer.init() bgm = pygame.mixer.Sound("music/bgm.wav") bgm.play()#play clock = pygame.time.Clock()#clock # Program start cycle execution while True:#loop # Fill background color screen.fill(backcolour) # Start interface if screens == 0:#False mod.show_back(0) for spr in news_group:#Traverse the sprites in the group spr.show()#Show sprites for event in pygame.event.get():#Traversal event if event.type == pygame.QUIT:#quit sys.exit()#Exit the system if event.type == pygame.MOUSEMOTION:#Coordinates of mouse movement for spr in news_group:#Sprite loop in group if spr.rect.collidepoint(event.pos):#Transfer coordinates outside the boundary of the rectangle spr.check = 0#False else: spr.check = 1#Otherwise it is true if event.type == pygame.MOUSEBUTTONDOWN:#mouse for spr in news_group: if spr.rect.collidepoint(event.pos): if spr.index == 0: # Click the start button level = 0#initialization first_draw = 1 timee = Time_countdown(time_limited[level])#Timing event_count_time = pygame.USEREVENT + 1#Every 1 change in time matchmen_change = pygame.USEREVENT + 2#Character transformation 2 pygame.time.set_timer(event_count_time, 1000)#Change every second pygame.time.set_timer(matchmen_change, 200)#Change every 200ms rotate = 1#initialization run = 0 wait = 0 miner_back = 0 special_change_speed = 0 change_man = True#Always true hook.reset()#hook screens = 1 finish = 0 scores.score = 0#integral elif spr.index == 1:#If you pass screens = 2 elif spr.index == 2:#If you fail sys.exit() pygame.QUIT#quit # Game operation interface elif screens == 1: mod.show_back(1) # Show different backgrounds if first_draw: timee.reset(time_limited[level]) # Retime first_draw = 0 miner_group = mod.set(level) # Event detection (keyboard handling) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN: # If the direction key is pressed run = 1 rotate = 0 if event.type == event_count_time: timee.miner() if event.type == matchmen_change: if run: man.change() # Connection between object and hook after collision if pygame.sprite.spritecollide(hook, miner_group, False, pygame.sprite.collide_mask): miner = pygame.sprite.spritecollide(hook, miner_group, False, pygame.sprite.collide_mask) if miner.first: miner.first = False ayis = (hook.rect.top + hook.rect.height / 2) + (miner.rect.height / 2) * math.sin( math.radians(hook.angle)) axis = (hook.rect.left + hook.rect.width / 2) - (miner.rect.height / 2) * math.cos( math.radians(hook.angle)) miner.rect.top = ayis - miner.rect.height / 2 miner.rect.left = axis - miner.rect.width / 2 hook.direction = [-hook.direction, -hook.direction] miner.direction = hook.direction if miner.speed >= 1: FPS *= miner.speed else: special_change_speed = 1 hook.speed = miner.speed miner_back = 1 if rotate: if hook.angle == 170: hook.turn_direct = -1 elif hook.angle == 10: hook.turn_direct = 1 hook.rotate(hook.turn_direct) if run: if hook.rect.left <= 0 or hook.rect.left + hook.rect.width >= screen_size[ 0] or hook.rect.top + hook.rect.height >= screen_size: hook.direction = [-hook.direction, -hook.direction] elif hook.rect.top < hook.ini_top: run = 0 rotate = 1 hook.reset() if miner_back: miner_back = 0 scores.score += int(miner.value) miner_group.remove(miner) FPS = 60 special_change_speed = 0 continue elif miner_back and special_change_speed: if (-hook.rect.left + hook.ini_left) != 0: r = math.atan((hook.rect.top - hook.ini_top) / (-hook.rect.left + hook.ini_left)) hook.direction = [10 * math.cos(r), -10 * math.sin(r)] if (r < 0): hook.direction = [-hook.direction, -hook.direction] hook.move() pygame.draw.line(screen, (0, 0, 0), (hook.rect.left + hook.rect.width / 2, hook.rect.top + hook.rect.height / 2), (hook.ini_left + hook.rect.width / 2, hook.ini_top + hook.rect.height / 2), 4) if miner_back: miner.direction = hook.direction miner.move(hook.speed) miner.rect = screen.blit(miner.img, miner.rect) for i in miner_group: if i.index != 4: break else: finish = 1 if len(miner_group) == 0 or timee.now == 0 or finish: if scores.score >= goals[level] and level <= 2: level += 1 screens = 4 if level == 3: continue else: screens = 3 timee.reset(time_limited) finish = 0 for spr in miner_group: spr.rect = screen.blit(spr.img, spr.rect) mod.show_txt(str(goals[level]), screen_size / 19 * 14, screen_size / 15) timee.show() scores.show() man.show() hook.rect = screen.blit(hook.load, hook.rect) # Rules interface elif screens == 2: mod.show_back(2) for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: screens = 0 if event.type == pygame.QUIT: sys.exit() # Failure interface elif screens == 3: mod.show_back(3) for event in pygame.event.get(): if event.type == (pygame.MOUSEBUTTONDOWN or pygame.KEYDOWN): screens = 0 if event.type == pygame.QUIT: sys.exit() # Success interface if screens == 4: mod.show_back(4) for event in pygame.event.get(): if event.type == (pygame.MOUSEBUTTONDOWN or pygame.KEYDOWN): if level >= 3: screens = 0 else: screens = 1 first_draw = 1 hook.reset() scores.score = 0 if event.type == pygame.QUIT: sys.exit() pygame.display.update() fclock.tick(FPS)
2. run on the ECS
Poor operation.. python3.5.2 is installed, and the pilot and pygame are installed with pip, but it seems that an error is reported, and the problem can not be solved by checking the information on the Internet..
The python version queried last is still 2.7 (┳ Д ┳)
The pilot is not installed either
I'm a loser fivefivefivefivefive
3, Course and experiment summary
The main contents of this semester are
(1) Variable assignment and its naming rules
(2) Operator and its priority
(3) Basic data type
(4) Loop statement
(5) Lists, tuples, dictionaries, collections and their common functions
(6) Strings and regular expressions
(7) Object oriented programming
(8) File operation and exception handling
(9) Python operation database
(10) Python crawler crawls to the desired content
I really didn't expect that the final experiment was so difficult that I couldn't run perfectly on the cloud server for several days. However, during this period, I learned a lot, such as learning some common sentences for playing games and some common command sentences, which deepened my understanding of the computer.
It's really painful to do some complicated things in a field you don't understand, but what you learn is often more profound and profound than what you learn from scratch, and it can also make you quickly master a skill.
The last experiment was filled with emotion. I recalled the stumbling along the way and felt that I had grown a lot. I felt the magic of a small code from the first time I opened a python book to the first time I typed 'hello world' on the keyboard. Sometimes, the more I learn, the more I understand that I still have many shortcomings. For Python and myself, I just feel that I am at the beginning of my introduction.
Admittedly, it is not easy to learn anything, but it just reflects the charm of learning. It is full and happy on the way to growth.
I still have a long way to go in learning python. Just as Chuang Tzu said, "my life is limited, and my knowledge is limitless."
This python learning has come to an end, but there is still more to come. May we have a long journey.