This article mainly introduces “Python+Pygame How to realize the crazy fruit eating game ”, In daily operation , I believe a lot of people are Python+Pygame There are doubts about how to achieve the crazy fruit eating game , Xiao Bian consulted all kinds of materials , Sort out simple and easy-to-use operation methods , I hope to answer ”Python+Pygame How to realize the crazy fruit eating game ” Your doubts help ! Next , Please follow Xiaobian to learn !
Random drop : watermelon ???? Bonus points 、 grapes ???? points 、 bomb ???? A health value is initially two . Move the right mouse button . How many points you can add or subtract will be up to you , It's not fun to be spoiled ! Every game code will give you some background , Hee hee , Feel for yourself ~
The environment used by Xiaobian :Python3、Pycharm Community Edition 、tkinter、Pygame modular , Partly from No display with module .
Module installation :pip install -i https://pypi.douban.com/simple/+ Module name
The background music is more exciting ! Remember seven This song , It's very pleasant .
Prepared materials, pictures, objects falling from the background, etc .
There are so many codes ! Just show the part
The main program
import tkinterimport randomimport timeimport Paramimport Imageimport Bonusimport Deductionimport Beanimport Bombimport pygame# Define the list of substances ( Including plus watermelon and minus grapes and bombs )bonusth = []deductionth = []bigbombs = []# Definition bean Variable , Save Doudou object bean = ""# Define the initial score of the current user score = 0life = 2# Define the game state game_state = Param.GAME_START # Create a form game_window = tkinter.Tk()# Window text settings game_window.title('I LOVE FRUIT')# Window position processing screenwidth = game_window.winfo_screenwidth()screenheight = game_window.winfo_screenheight()size = '%dx%d+%d+%d' % (Param.GAME_WIDTH, Param.GAME_HEIGHT, (screenwidth-Param.GAME_WIDTH)/2, 50)game_window.geometry(size)# Load all the pictures used in the game background_image,bean_image,Bonus_image,Bomb_image,Deduction_image= Image.load_image(tkinter)Start,Stop = Image.load_state_image(tkinter) # Get canvas window_canvas = tkinter.Canvas(game_window)# Canvas packaging window_canvas.pack(expand=tkinter.YES, fill=tkinter.BOTH) # Time stamp count = 0num = 30 def create_fruit():# Produce fruit global count global num global score if score % 10 ==1: if num >= 8: num -= 8 count += 1 if count % num == 0: c = random.randint(1,10) if c <= 5: # Bonus fruit generation bonus = Bonus.Bonus(Bonus_image) bonusth.append(bonus) # Substances added to the list window_canvas.create_image(bonus.x,bonus.y,anchor = tkinter.NW,image=bonus.image,tag=bonus.tag) elif c<=8: # Distribution of fruit production deduction = Deduction.Deduction(Deduction_image) deductionth.append(deduction) window_canvas.create_image(deduction.x,deduction.y,anchor = tkinter.NW,image=deduction.image,tag=deduction.tag) else: # Bomb generation bigbomb = Bomb.BigBomb(Bomb_image) bigbombs.append(bigbomb) window_canvas.create_image(bigbomb.x,bigbomb.y,anchor = tkinter.NW,image=bigbomb.image,tag=bigbomb.tag) def step_fruit(): # Traverse all matter , Call the move method for bonus in bonusth: bonus.step(window_canvas) for deduction in deductionth: deduction.step(window_canvas) for bigbomb in bigbombs: bigbomb.step(window_canvas) def judge_state(event): global game_state if game_state == Param.GAME_START: game_state = Param.GAME_RUNNING # Draw points window_canvas.create_text(20, 20, text=" fraction :%d" % (score), anchor=tkinter.NW, fill="white",\ font="time 12 bold",tag="SCORE") # Painting life window_canvas.create_text(20, 50, text=" life :%d" % (life), anchor=tkinter.NW, fill="white",\ font="time 12 bold",tag="LIFE") # Delete startup picture window_canvas.delete("Start") elif game_state == Param.GAME_STOP: window_canvas.delete("bean") window_canvas.delete("STOP") game_state = Param.GAME_START game_start() def bean_move(event): if game_state == Param.GAME_RUNNING: now_x = bean.x now_y = bean.y bean.x = event.x - bean.w/2 bean.y = event.y - bean.h/2 window_canvas.move("bean", bean.x-now_x, bean.y-now_y) def out_of_bounds(): # Get all the substances , Judge whether you crossed the line for deduction in deductionth: if deduction.out_of_bounds(): window_canvas.delete(deduction.tag) deductionth.remove(deduction) for bonus in bonusth: global outnum if bonus.out_of_bounds(): outnum += 1 window_canvas.delete(bonus.tag) bonusth.remove(bonus) if outnum >= 5: game_state = Param.GAME_STOP # Draw the end of the game game_over() for bigbomb in bigbombs: if bigbomb.out_of_bounds(): window_canvas.delete(bigbomb.tag) bigbombs.remove(bigbomb) def bomb_action(): global score global life global bean global game_state # Bonus points for bonus in bonusth: if bonus.bomb(bean): window_canvas.delete(bonus.tag) bonusth.remove(bonus) score += 3 # points for deduction in deductionth: if deduction.bomb(bean): window_canvas.delete(deduction.tag) deductionth.remove(deduction) if score - 5 < 0: score = 0 game_state = Param.GAME_STOP # Draw the end of the game game_over() else: score -= 5 for bigbomb in bigbombs: if bigbomb.bomb(bean): window_canvas.delete(bigbomb.tag) bigbombs.remove(bigbomb) # If the score or life is less than 0 Game over if life - 1 <= 0: life = 0 game_state = Param.GAME_STOP # Draw the end of the game game_over() else: life -= 1 def draw_action(): # Draw points window_canvas.delete("SCORE") # Painting life window_canvas.delete("LIFE") window_canvas.create_text(20,20,text=" fraction :%d"%(score),anchor=tkinter.NW,fill="white",font="time 12 bold",tag="SCORE") window_canvas.create_text(20,50,text=" life :%d"%(life),anchor=tkinter.NW,fill="white",font="time 12 bold",tag="LIFE") def game_over(): global game_state game_state = Param.GAME_STOP for deduction in deductionth: window_canvas.delete(deduction.tag) for bonus in bonusth: window_canvas.delete(bonus.tag) for bigbomb in bigbombs: window_canvas.delete(bigbomb.tag) deductionth.clear() bonusth.clear() bigbombs.clear() window_canvas.create_image(0,0,anchor=tkinter.NW,image=Stop,tag="STOP") if pygame.mixer.music.get_busy() == True: pygame.mixer.music.stop()# Stop playing def game_start(): global score global life global num global outnum num = 30 score = 0 life = 2 outnum = 0 # Draw game background window_canvas.create_image(0, 0, anchor=tkinter.NW, image=background_image, tag="background") # Create a Doudou object global bean bean = Bean.Bean(bean_image) window_canvas.create_image(bean.x, bean.y, anchor=tkinter.NW, image=bean.image, tag="bean") window_canvas.create_image(0, 0, anchor=tkinter.NW, image=Start, tag="Start") pygame.mixer.init() pygame.mixer.music.load('Seve( Piano version ).mp3') # Load background music if pygame.mixer.music.get_busy() == False: pygame.mixer.music.play(300,0)# repeat 300 Time , Play from the first second def game(): if game_state == Param.GAME_START: game_start() # Mouse monitor window_canvas.bind("<Motion>",bean_move) window_canvas.bind("<Button-1>",judge_state) while True: if game_state == Param.GAME_RUNNING: # Material entry create_fruit() # Matter moves step_fruit() # Delete substances that cross the boundary out_of_bounds() # Detect collision bomb_action() if score >= 0: # Painting and life draw_action() # Update display game_window.update() time.sleep(0.04) if __name__ == "__main__": game() game_window.mainloop()
1) Game interface
2) Random screenshot
3) Consumption ends
Here we are , About “Python+Pygame How to realize the crazy fruit eating game ” That's the end of my study , I hope we can solve your doubts . The combination of theory and practice can better help you learn , Let's try ! If you want to continue to learn more related knowledge , Please continue to pay attention to Yisu cloud website , Xiaobian will continue to strive to bring you more practical articles !