Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the game of big fish eating small fish by Python

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "Python how to realize the game of big fish eating small fish". The editor shows you the operation process through the actual case, the operation method is simple and fast, and the practicality is strong. I hope this article "how to realize the game of big fish eating small fish in Python" can help you solve the problem.

one。 Game screen

two。 Game material

three。 Program introduction

Big fish eat small fish. Py

Note that the program's mouth object is not "hidden", although it is invisible.

Small fish will be "eaten" when they encounter mouth. If the mouth is hidden with the hide command, the binding box of the mouth cannot be obtained, and the collision detection is invalid.

four。 Game code 1. Elf object. This function calculates a coordinate in the lower right corner of the rectangle and returns it from sprites import * def calculate_pos (obj): "obj: sprite object. This function calculates a coordinate in the lower right corner of the rectangle and returns it. "" x else y = obj.position () # coordinates of the character mx,my = mouse_position () # coordinates of the mouse pointer k = 1 if mx > x else-1 # is 1 on the right Otherwise,-1 left,top,right Bottom = obj.bbox () # get the width of the binding box w = right-left # the width of the big fish h = top-bottom # the height of the big fish x0 = x + k * wUnip 2.5 # the approximate x coordinate of the mouth Y0 = y-h Charpy 12 # the approximate y coordinate of the mouth return x0LY 02. Set game properties width,height = 480360 screen = Screen () # New width and height screen.setup (width,height) # set width and height screen.bgpic ('res/underwater.png') # set background image screen.title ("big fish of Turing sea eats small fish") 3. Game object fish_group = Group (tag='fish') # New Group The tag is fishfishes = ['res/fish2.png','res/fish3.png','res/fish4.png','res/crab-b.png'] # because the following fish tags are all fish, so they are automatically added to fish_group for x in range (10): X = random.randint (- 200200) y = random.randint (- 140140) f = Sprite (shape=random.choice (fishes), tag='fish',pos= (x) ) f.scale [fish.setheading (random.randint (1360)) for fish in fish_group] M1 = Mouse (1) # left mouse button fish = Sprite ('res/fish2-a.png') # instantiate big fish fish.rotatemode (1) # flip fishscale= 0.6fish.scale (fishscale) mouth = Sprite (shape='circle') # instantiated mouth For collision detection mouthscale = 0.4mouth.scale (mouthscale) # scale mouth size mouth.setalpha (0) # set it to transparent, change it to non-0 it will display clock = Clock () # New clock object 4. Dynamic effects of the game while True: for f in fish_group: if f.isvisible (): f.fd (1) # move only when visible # small fish touch the mouth and click the mouse will be eaten Big fish grow up if f.collide (mouth) And m1.down (): fishscale + = 0.01fish.scale (fishscale) # Big fish grow up mouthscale + = 0.01mouth.scale (mouthscale) # mouth enlarged x = random.randint (- 200200) y = random.randint (- 140140) # Note here after reborn is called The fish will hide immediately and appear after 3. # the collision detection is invalid within 3 seconds. So the fish can't move f.reborn (XGramage yGrams3) f.shape (random.choice (fishes)) f.bounce_on_edge () fish.heading (mouse_pos ()) # Big fish follow the mouse pointer x0memery y0 = calculate_pos (fish) # calculate the approximate coordinates of the mouth mouth.goto (x0) Y0) # big mouth this coordinate md = fish.distance (mouse_pos ()) # calculate the distance from fish to mouse pointer if md > 50:fish.fd (min (md) 4)) # if the distance is greater than 50, swim # if m1.down (): fish.shape ('res/fish2-a.png') else: fish.shape (' res/fish2-b.png') screen.update () clock.tick (60) fish.shape ('res/fish2-a.png') else: fish.shape (' res/fish2) -b.png') screen.update () clock.tick (60) about "how Python realizes the game of eating big fish and eating small fish" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report