preface
hello everyone! I'm classmate pear!
I hope you can support me! ha-ha
To thank everyone who cares about me: 💓 The project source code of each article is shared free of charge 💓👇👇👇
Xiaobian has also been learning programming. If there are errors in the code applet, please leave a message in the comment area!
Finally - if the article helps you, remember to "pay attention", "like" and "comment"~
text
With the arrival of winter, it is the best season to pick strawberries in the year.
Xiaobian has been to La several times at the weekend. It's nice, ha ha ha jpg but if you are in a Strawberry Garden, you need to make a good choice
Pull, I stepped on the pit before. Speaking of strawberries 🍓 If so, today Xiaobian will make a fruit cutting game for you!
Environment installation——
1) Prepare the corresponding identified pictures. Here are the material pictures randomly searched on the Internet!
2) The python 3.0 environment is ready for installation, and basically all Python versions can be edited 7. Pycharm2021, game module
Pygame, and then some of its own can be imported directly without tube!
The common method of installing modules, that is, the small compilation of third-party modules, is: pip install + module name or speed up requires the use of image source,
Baidu or csdn search will come out a lot of image sources for installing modules, which will not be introduced one by one here!
1, In preparation
1) Material background
This is the background picture of the overall interface applet. You can find it yourself. The Xiaobian here looks for it as follows 👇
There are also pictures of various fruits. You can find them freely!
Complete source code:
import pygame, sys import os import random player_lives = 3 # life score = 0 # score fruits = ['melon', 'orange', 'pomegranate', 'guava', 'bomb'] # Fruit and bombs # Game window WIDTH = 800 HEIGHT = 500 FPS = 12 # The frame rate of gameDisplay is refreshed once every 1 / 12 seconds pygame.init() pygame.display.set_caption('Speed cut fruit games') # title gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT)) # Game window clock = pygame.time.Clock() # Colors used WHITE = (255,255,255) BLACK = (0,0,0) RED = (255,0,0) GREEN = (0,255,0) BLUE = (0,0,255) background = pygame.image.load('Background map/03.png') # background font = pygame.font.Font(os.path.join(os.getcwd(), 'typeface/comic.ttf'), 42) # typeface score_text = font.render('Score : ' + str(score), True, (255, 255, 255)) # Font style for score # Location and data storage of randomly generated fruits def generate_random_fruits(fruit): fruit_path = "images/" + fruit + ".png" data[fruit] = { 'img': pygame.image.load(fruit_path), 'x' : random.randint(100,500), # Position of fruit on the x-axis 'y' : 800, 'speed_x': random.randint(-10,10), # Speed and diagonal movement of fruit in x direction 'speed_y': random.randint(-80, -60), # Velocity in y direction 'throw': False, # If the location of the generated fruit is outside the gameDisplay, it will be discarded 't': 0, 'hit': False, } if random.random() >= 0.75: # Returns the next random floating-point number in the range of [0.0, 1.0] to maintain the display of fruit in the game. data[fruit]['throw'] = True else: data[fruit]['throw'] = False # Use a dictionary to store fruit data data = {} for fruit in fruits: generate_random_fruits(fruit) def hide_cross_lives(x, y): gameDisplay.blit(pygame.image.load("images/red_lives.png"), (x, y)) # Draw fonts on the screen font_name = pygame.font.match_font('comic.ttf') def draw_text(display, text, size, x, y): font = pygame.font.Font(font_name, size) text_surface = font.render(text, True, WHITE) text_rect = text_surface.get_rect() text_rect.midtop = (x, y) gameDisplay.blit(text_surface, text_rect) # Draw player's life def draw_lives(display, x, y, lives, image) : for i in range(lives) : img = pygame.image.load(image) img_rect = img.get_rect() img_rect.x = int(x + 35 * i) img_rect.y = y display.blit(img, img_rect) # Game start and end screen def show_gameover_screen(): gameDisplay.blit(background, (0,0)) draw_text(gameDisplay, "FRUIT NINJA!", 90, WIDTH / 2, HEIGHT / 4) if not game_over : draw_text(gameDisplay,"Score : " + str(score), 50, WIDTH / 2, HEIGHT /2) draw_text(gameDisplay, "Press any key to start the game", 64, WIDTH / 2, HEIGHT * 3 / 4) pygame.display.flip() waiting = True while waiting: clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() if event.type == pygame.KEYUP: waiting = False # Game main loop first_round = True game_over = True # More than 3 bombs, terminate the game cycle game_running = True # Manage game cycles while game_running : if game_over : if first_round : show_gameover_screen() first_round = False game_over = False player_lives = 3 draw_lives(gameDisplay, 690, 5, player_lives, 'images/red_lives.png') score = 0 for event in pygame.event.get(): # Check whether the window is closed if event.type == pygame.QUIT: game_running = False gameDisplay.blit(background, (0, 0)) gameDisplay.blit(score_text, (0, 0)) draw_lives(gameDisplay, 690, 5, player_lives, 'images/red_lives.png') for key, value in data.items(): if value['throw']: value['x'] += value['speed_x'] # Move the fruit in the x direction value['y'] += value['speed_y'] # Move in y direction value['speed_y'] += (1 * value['t']) # Increasing value['t'] += 1 if value['y'] <= 800: gameDisplay.blit(value['img'], (value['x'], value['y'])) # Dynamic display of fruit else: generate_random_fruits(key) current_position = pygame.mouse.get_pos() # Gets the position of the mouse in pixels if not value['hit'] and current_position[0] > value['x'] and current_position[0] < value['x']+60 \ and current_position[1] > value['y'] and current_position[1] < value['y']+60: if key == 'bomb': player_lives -= 1 if player_lives == 0: hide_cross_lives(690, 15) elif player_lives == 1 : hide_cross_lives(725, 15) elif player_lives == 2 : hide_cross_lives(760, 15) # More than 3 bombs, prompt the end of the game and reset the window if player_lives < 0 : show_gameover_screen() game_over = True half_fruit_path = "images/explosion.png" else: half_fruit_path = "images/" + "half_" + key + ".png" value['img'] = pygame.image.load(half_fruit_path) value['speed_x'] += 10 if key != 'bomb' : score += 1 score_text = font.render('Score : ' + str(score), True, (255, 255, 255)) value['hit'] = True else: generate_random_fruits(key) pygame.display.update() clock.tick(FPS) pygame.quit()
Effect display:
Game rules: click the corresponding fruit mouse to add points. When you click the thunder, one HP will disappear, and the total three HP will disappear
Beam!
Game interface——
The game is running——
It's thunder——
Game over——
Summary
All right! Quick cut fruit 🍓 The little game is over here. I can pull the old rule source code private letter!
Follow Xiaobian for more wonderful content!
It's not easy to make. Remember to click three times!! If you need to pack the source code + materials, share them for free!! Portal