(journalism) lede
Hello! Hello~ I'm Kimiko, it's been a long time since I've updated you all on the game's similarities--
A fan contributed, said recently due to the impact of the epidemic closed at home can not leave the community, not long ago just signed up for the driver's license test can only be postponed wirelessly, in the home is still quite boring, "suffocated in the home nothing to do when you go to play games and write code is also quite good.
So, I had a brainwave and came up with today's easy version of The Racing Program aka cough cough .......
Common point is a code to write a small game about the car, due to the editor's thinking and programming learning can not keep up!
SO, I tried my best ...... I can only show a racing game like this~ If you have a god, you can optimize it!
I. Environmental installation
1. Various materials (pictures)
2. Operating environment
The environment used by the editor: Python3, Pycharm Community Edition, Pygame module partially self-contained.
Module installation: pip install -i /simple/ pygame
II. Code presentation
This game code is particularly much, I here mainly show the main program to run the code, all the find me at the end of the article to get Ha ~!
1. Main program
import os, sys, pygame, random, array, gamemode import direction, bounds, timeout, menu from import * #Import game modules. from loader import load_image import player, maps, traffic, camera, tracks TRAFFIC_COUNT = 45 CENTER_W = -1 CENTER_H = -1 #Main function. def main(): #initialize objects. clock = () running = True font = (None, 24) car = () cam = () target = () bound_alert = () time_alert = () info = () pointer = (int(CENTER_W * 2), int(CENTER_H * 2)) #create sprite groups. map_s = () player_s = () traffic_s = () tracks_s = () target_s = () pointer_s = () timer_alert_s = () bound_alert_s = () menu_alert_s = () #generate tiles for tile_num in range (0, len(maps.map_tile)): maps.map_files.append(load_image(maps.map_tile[tile_num], False)) for x in range (0, 10): for y in range (0, 10): map_s.add((maps.map_1[x][y], x * 1000, y * 1000, maps.map_1_rot[x][y])) #load tracks () #load finish target_s.add(target) #load direction pointer_s.add(pointer) #load alerts timer_alert_s.add(time_alert) bound_alert_s.add(bound_alert) menu_alert_s.add(info) #load traffic (CENTER_W, CENTER_H) for count in range(0, TRAFFIC_COUNT): traffic_s.add(()) player_s.add(car) cam.set_pos(, ) while running: #Render loop. #Check for menu/reset, (keyup event - trigger ONCE) for event in (): if == : if keys[K_m]: if ( == True): = False else: = True if (keys[K_p]): () () if (keys[K_q]): () (0) if == and == pygame.K_ESCAPE: running = False break #Check for key input. (KEYDOWN, trigger often) keys = .get_pressed() if ( > 0): if keys[K_LEFT]: () if keys[K_RIGHT]: () if keys[K_UP]: () else: () if keys[K_DOWN]: () cam.set_pos(, ) #Show text data. text_fps = ('FPS: ' + str(int(clock.get_fps())), 1, (224, 16, 16)) textpos_fps = text_fps.get_rect(centery=25, centerx=60) text_score = ('Score: ' + str(), 1, (224, 16, 16)) textpos_score = text_fps.get_rect(centery=45, centerx=60) text_timer = ('Timer: ' + str(int(( / 60)/60)) + ":" + str(int(( / 60) % 60)), 1, (224, 16, 16)) textpos_timer = text_fps.get_rect(centery=65, centerx=60) #Render Scene. (background, (0,0)) #cam.set_pos(, ) map_s.update(, ) map_s.draw(screen) #Conditional renders/effects (screen.get_at(((int(CENTER_W-5), int(CENTER_H-5)))).g) if (): tracks_s.add(( + CENTER_W, + CENTER_H, )) #Just render.. tracks_s.update(, ) tracks_s.draw(screen) player_s.update(, ) player_s.draw(screen) traffic_s.update(, ) traffic_s.draw(screen) target_s.update(, ) target_s.draw(screen) pointer_s.update( + CENTER_W, + CENTER_H, , ) pointer_s.draw(screen) #Conditional renders. if ((+CENTER_W, +CENTER_H) == True): bound_alert_s.update() bound_alert_s.draw(screen) if ( == 0): timer_alert_s.draw(screen) = 0 text_score = ('Final Score: ' + str(), 1, (224, 16, 16)) textpos_score = text_fps.get_rect(centery=CENTER_H+56, centerx=CENTER_W-20) if ( == True): menu_alert_s.draw(screen) #Blit Blit.. (text_fps, textpos_fps) (text_score, textpos_score) (text_timer, textpos_timer) () #Check collision!!! if (car, traffic_s, False): () target.car_crash() if (car, target_s, True): target.claim_flag() target.generate_finish() target_s.add(target) (64) #initialization () screen = .set_mode((().current_w, ().current_h), ) .set_caption('Race of Math.') .set_visible(False) font = (None, 24) CENTER_W = int(().current_w /2) CENTER_H = int(().current_h /2) #new background surface background = (screen.get_size()) background = background.convert_alpha() ((26, 26, 26)) #Enter the mainloop. main() () (0)
2.Map Settings
import os, sys, pygame, math from import * from loader import load_image from random import randrange #Map filenames. map_files = [] map_tile = ['', '', '', '', '', ''] #Map to tile. crossing = 0 straight = 1 turn = 2 split = 3 deadend = 4 null = 5 #tilemap. map_1 = [ [2,1,3,1,1,3,1,1,1,4], [1,5,1,5,4,0,1,2,5,4], [1,4,3,1,3,3,1,3,2,1], [3,1,3,1,3,5,4,5,1,1], [3,2,1,5,1,5,3,1,0,3], [1,2,0,1,0,3,0,4,1,1], [1,5,1,4,2,1,1,2,3,1], [1,2,0,1,3,3,0,0,2,1], [1,1,4,2,2,5,1,2,1,3], [2,3,1,3,1,1,3,1,1,2] ] #tilemap rotation, x90ccw map_1_rot = [ [1,1,0,1,1,0,1,1,1,3], [0,0,0,0,1,0,1,0,0,0], [0,1,2,1,0,2,1,2,0,0], [1,1,0,1,3,0,0,0,0,0], [1,0,0,0,0,0,1,1,0,3], [0,2,0,1,0,0,0,3,0,0], [0,0,0,1,3,0,0,1,3,0], [0,1,0,1,0,2,0,0,3,0], [0,0,2,1,3,0,0,2,1,3], [2,2,1,2,1,1,2,1,1,3] ] class Map(): def __init__(self, tile_map, y, x, rot): .__init__(self) = map_files[tile_map] = .get_rect() if rot != 0: = (, rot * 90) = x = y #Realign the map def update(self, cam_x, cam_y): = - cam_x, - cam_y
III. Effectiveness demonstration
Gameplay: M Game start - P Start over - Q Exit the game. Following the red arrows to run will bring up trophies. A trophy 15
The more trophies you get in the time limit, the higher the score!
1.Game Interface
2. The game is running
3.15 cents to go.
To this article on the realization of classic racing games based on Python Pygame article is introduced to this, more related Python Pygame racing game content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!