War epidemic interaction house home epidemic prevention, "destroy COVID-19" small game hot line up to (with complete source code)

Posted by tegwin15 on Fri, 07 Jan 2022 03:34:02 +0100

preface

Hello, hello! Today it's updated again 👇

I believe that during the epidemic period, my partners are the same as Xiaobian. Except for going to work, they just stay at home and contribute to the motherland

Under the new situation, the epidemic situation has been curbed, the trend has greatly improved, and everyone is very excited!

However, the overseas epidemic is becoming more and more serious

Recently, overseas imported cases have emerged in many places in China

Remind us again that prevention and control should not be loosened

Go out less, do not gather, and do a good job in personal protection

​​​

Xiaobian thinks that while we stay at home, we might as well calm down and read more books to improve our value! Or perhaps in their spare time to learn epidemic prevention

Knowledge, scientific and correct, learn personal protection!

In his spare time, Xiaobian also wrote a Python version of "virus elimination game" for everyone. When he is bored, everyone moves and plays together

Down~

text

1, Brief introduction

New Coronavirus bubble hall games. py

This program borrows the mixer module of pygame, so you need to install pygame module.

Wizard module version 1.33 support is required. Please uninstall the old version and reinstall the latest version of Wizard module.  

Installation method: the maze room in the pip install sprites program is drawn. Of course, insects can't cross the past or encounter viruses  

Press the up, down or left keys to operate the insect movement, and press the space bar to put a bomb.

2, Environmental preparation

(show only some materials)

3, Code demonstration

"""
 CSDN Account No.: Gu muzia    Remember to pay attention
 The official account of WeChat: Python Gu Muzi acridine learn the source code together and share it"""
 
from sprites import * 
from pygame import mixer
 
def draw_frame(obj,margin):
    """use obj Character draw border,margin Is the margin
       This function uses for You can also draw a square directly.
    """
    sw = obj.screen.window_width()
    sh = obj.screen.window_height()
    bug.topleft()            # Move to upper left corner
    bug.addy(-margin)   
    x,y = bug.position()  
    cors = [(x+sw,y),(x+sw,y+margin),(x,y+margin)]
    f1 = bug.polygon(cors)
    
    bug.bottomleft()         # Move to lower left corner      
    x,y = bug.position()  
    cors = [(x+sw,y),(x+sw,y+margin),(x,y+margin)]
    f2 = bug.polygon(cors)
    
    bug.addy(margin)    
    x,y = bug.position()  
    cors = [(x+margin,y),(x+margin,y+(sh-2*margin)),(x,y+(sh-2*margin))]
    f3 = bug.polygon(cors)
 
    bug.addx(sw-margin)    
    x,y = bug.position()  
    cors = [(x+margin,y),(x+margin,y+(sh-2*margin)),(x,y+(sh-2*margin))]
    f4 = bug.polygon(cors)
    return [f1,f2,f3,f4]
 
def draw_cross(obj,length):
    """Draw a cross"""
    d = length/2
    bug.goto(-d,d)
    x,y = bug.position()
    cors = [(x+length,y),(x+length,y+2*length),(x,y+2*length)] 
    p1 = bug.polygon(cors)
 
    bug.goto(-d-2*length,-d)
    x,y = bug.position()
    cors = [(x+5*length,y),(x+5*length,y+length),(x,y+length)] 
    p2 = bug.polygon(cors)
    
    bug.goto(-d,-d-2*length)    
    x,y = bug.position()
    cors = [(x+length,y),(x+length,y+2*length),(x,y+2*length)] 
    p3 = bug.polygon(cors)
    return [p1,p2,p3]
    
def draw_squares(obj,length):
    """Draw 4 squares"""
    items = []
    for _ in range(4):
        i = bug.polygon(4,length)
        items.append(i)
        bug.fd(length*5)
        bug.rt(90)
    return items
 
width = 800
height = 600
screen = Screen()
screen.bgcolor('black')
screen.setup(width,height)
screen.title('New Coronavirus bubble hall games')
screen.bgpic('cover.png')
 
leftkey = Key('Left')
rightkey = Key('Right')
upkey = Key('Up')
downkey = Key('Down')
spacekey = Key('space')
screen.listen()
 
PlaySound('audios/Opening music.wav',SND_ASYNC)
starttime = time.time()
while time.time() - starttime < 6 :
    screen.update()
    if spacekey.down():break
screen.bgpic('nopic')
PlaySound(None,SND_PURGE)
PlaySound('audios/Bubble Hall Community Music.wav',SND_ASYNC|SND_LOOP)
 
mixer.init()                      # Initialize mixer
bombsound = mixer.Sound('audios/BOMB1.wav')
placesound = mixer.Sound('audios/zoop.wav')
 
bomb = Sprite('bombs',visible=False)
bomb.scale(0.5)
 
w = 100
bug = Sprite(visible=False)
bug.color('pink','pink')
items1 = draw_frame(bug,w/2)      # Draw a border next to the screen
items2 = draw_cross(bug,w/1.5)    # Draw a cross
bug.goto(-w/2 - w,w/3 + w)
items3 = draw_squares(bug,w/1.5)  # Draw four squares
items1.extend(items2)
items1.extend(items3)
print(items1)                     # items1 is the item number of the obstacle list
frames = items1                   # Take an alias
 
bug.color('gray')
bug.goto(0,240)
info = '''The game code has 210 lines, Python Wizard module development,author:Li Xingqiu,
The maze is drawn,Press the up, down, left and right keys to operate the insect movement,Press the space bar to put the bomb.
'''
bug.write(info,align='center')
bug.color('black')
bug.goto(0,-290)
info = '''Python The Wizard module is developed based on the turtle drawing module,
The author is also Li Xingqiu. Blog: www.lixingqiu.com'''
bug.write(info,align='center')
 
bug.goto(100,100)
clock = Clock()
bug.show()
 
cors = [(-100,100),(-100,-100),(265,2),(179,-107),(300, 101),
        (0,-219),(273,-213),(263, -140),(-258, 0),(-116,222)]
vsitems = []                       # Stores a list of each virus item number
vsgroup = Group('virus')           # Virus group
for cor in cors:
    virus = Sprite(shape='res/virus.png',pos=cor,tag='virus')
    virus.rotatemode(1)
    vsitems.append(virus.turtle._item)
    
result = None                      # Describe the success or failure of the game 
running = True                     # Describe the main cycle of the game
allow = True                       # Can you put explosives?
counter = 0                        # Modeling counter of explosive
def check_bomb():
    global counter,allow,running
    if running == False:return
    if bomb.isvisible():
        if counter == 0 :
            screen.ontimer(check_bomb,1000)
            counter += 1
        elif counter < len(bomb._costumes):
           bomb.nextcostume()          
           if counter < 3:
               screen.ontimer(check_bomb,1000)               
           else:
               if counter==3:bombsound.play()               # Explosion sound
               screen.ontimer(check_bomb,100)           
           counter += 1
        elif counter == len(bomb._costumes):
           bomb.hide()
           bomb.shapeindex(0)
           allow = True
           counter = 0
           screen.ontimer(check_bomb,100)
    else:
        screen.ontimer(check_bomb,100)
check_bomb()
 
while running:    
    if leftkey.down():
        bug.setheading(180)
        bug.fd(5)
        if bug.overlap_with(items1):bug.bk(5)
    if rightkey.down():
        bug.setheading(0)
        bug.fd(5)
        if bug.overlap_with(items1):bug.bk(5)
    if upkey.down():
        bug.setheading(90)
        bug.fd(5)
        if bug.overlap_with(items1):bug.bk(5)
    if downkey.down():
        bug.setheading(-90)
        bug.fd(5)
        if bug.overlap_with(items1):bug.bk(5)
    if spacekey.down() and allow == True :
        print('Bomb')
        placesound.play()
        bomb.goto(bug.position())
         
        bomb.show()
        allow = False
    for v in list(vsgroup):                 # Prevent dynamic changes to vsgroup, so add list
        v.fd(2)
        if v.overlap_with(frames):v.right(180)
        if v.overlap_with(bomb) and bomb._costume_index>2:
            v.remove()
        
    if bug.overlap_with(bomb) and bomb._costume_index > 2:
        running = False
        result = False
 
    if bug.overlap_with(vsitems):
        running = False
        result = False
    if len(vsgroup) == 0 :
         running = False
         result = True
    screen.update()
    clock.tick(60)
 
print(result)
bug.wait(0.5)
if result:
    print('Show successful cover pages')
    screen.clear()
    screen.bgpic('success.png')
else:
    print('Show failed cover')
    screen.clear()
    screen.bgpic('fail.png')
    
screen.mainloop()

4, Effect display

1) Screenshot display——

Game interface 👇

Game interface 👇

bomb 💣👇

game over 👇

Summary

At present, the epidemic is not over, and we can't take it lightly! Try not to go out and gather, and do a good job of personal protection. Firm confidence and never relax

Pay close attention to all prevention and control work, prevent external input and internal proliferation, and resolutely win this battle.

Complete material, etc.: didi I acridine!

There are more source code waiting for you to lead the area!

Article summary——

Summary: Python article collection | (Introduction to actual combat, games, Turtle, cases, etc.)

Topics: Python Mini Program pygame