development tool
python version: 3.6.4
Related Modules
pygame; and some of python's own modules.
Environment Setup
Just install python and add it to the environment variables, and pip install the relevant modules you need.
Introduction to the Principle
I believe we all know the rules of the game of gopher, here will not introduce more, anyway, is to keep hitting the gopher drilled out of the hole with a hammer ~ first of all, let's determine what elements of the game. The first thing we need to do is to determine what elements are in the game. Gopher playing gopher playing gopher, of course, gopher must have it, so we will write a gopher game sprite class Obviously, the gopher has been hit by the hammer and not been hit by the hammer in these two states, so you need to load two diagrams, when the gopher is hit by the gopher from not being hit by the state of the gopher map switched to the state of the gopher map after being hit by the gopher (I find the picture may not be very gopher, please forgive me). Then we define the hammer game sprite class, and the gopher is similar, the hammer also has not hammered down and has been hammered down two states, only after the hammer down need to quickly return to the state of not hammered down, in particular, the code implementation is as follows.
Primary Code
Game Initialization
def initGame(): () () screen = .set_mode() .set_caption('Python QQ exchange group: 932574150') return screen
ok, will be ready to do a good job, we can start writing the main program. First of all, of course, our game initialization ah:
initialization
screen = initGame()
Then we're going to load up our necessary game materials and necessary game variables.
# Load background music and other sound effects (cfg.BGM_PATH) (-1) audios = { 'count_down': (cfg.COUNT_DOWN_SOUND_PATH), 'hammering': (cfg.HAMMERING_SOUND_PATH) } # Load fonts font = (cfg.FONT_PATH, 40) # Load background image bg_img = (cfg.GAME_BG_IMAGEPATH) # Start screen startInterface(screen, cfg.GAME_BEGIN_IMAGEPATHS) # Timing of gopher changes of position hole_pos = (cfg.HOLE_POSITIONS) change_hole_event = .set_timer(change_hole_event, 800) # Gopher mole = Mole(cfg.MOLE_IMAGEPATHS, hole_pos) # Hammer hammer = Hammer(cfg.HAMMER_IMAGEPATHS, (500, 250)) # Clock clock = () # Scores your_score = 0 flag = False
I've annotated it so you can see it.
Now it's time for our main game loop.
# The main game loop while True: # -- game time is 60s time_remain = round((61000 - .get_ticks()) / 1000.) # - game time is reduced, gophers change positions faster # if time_remain == 40 and not flag: hole_pos = (cfg.HOLE_POSITIONS) () (hole_pos) .set_timer(change_hole_event, 650) flag = True elif time_remain == 20 and flag: hole_pos = (cfg.HOLE_POSITIONS) () (hole_pos) .set_timer(change_hole_event, 500) flag = False # -- countdown sound effects if time_remain == 10: audios['count_down'].play() # -- game over if time_remain < 0: break count_down_text = ('Time: '+str(time_remain), True, ) # --Keystroke Detection for event in (): if == : () () elif == : (.get_pos()) elif == : if == 1: () elif == change_hole_event: hole_pos = (cfg.HOLE_POSITIONS) () (hole_pos) # -- collision detection if hammer.is_hammering and not mole.is_hammer: is_hammer = .collide_mask(hammer, mole) if is_hammer: audios['hammering'].play() () your_score += 10 # -- fraction your_score_text = ('Score: '+str(your_score), True, ) # -- Bind the necessary game elements to the screen (note the order) (bg_img, (0, 0)) (count_down_text, (875, 8)) (your_score_text, (800, 430)) (screen) (screen) # --update () (60)
The logic is very simple, so I won't bore you with all the nonsense, but let's take a look at my notes. After 60 seconds, the game is over, and we can tally up the scores and compare them to the all-time highs:
# Read the best score (try block to avoid no .rec file for the first game) try: best_score = int(open(cfg.RECORD_PATH).read()) except: best_score = 0 # If current score is greater than optimal score update optimal score if your_score > best_score: f = open(cfg.RECORD_PATH, 'w') (str(your_score)) ()
Then we'll make our game a little more formal and ceremonial by adding a start screen and an end screen:
# End screen score_info = {'your_score': your_score, 'best_score': best_score} is_restart = endInterface(screen, cfg.GAME_END_IMAGEPATH, cfg.GAME_AGAIN_IMAGEPATHS, score_info, cfg.FONT_PATH, [, ], ) return is_restart
Finally, add the runtime code
if __name__ == '__main__': while True: is_restart = main() if not is_restart: break
To this article on the Python implementation of the gopher game is introduced to this article, more related Python gopher 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!