• Python版消灭病毒、消灭新冠小游戏源代码


    Python版消灭病毒、消灭新冠小游戏源代码,上下方向键控制战斗少女,如果被病毒袭击将会Game Over,按空格键发射火球烧死病毒获得积分。
    开始界面
    在这里插入图片描述
    游戏界面
    在这里插入图片描述
    核心代码:
    game.py

    # PyGame template.
     
    # Import standard modules.
    
    import sys
    import os
    from matplotlib.pyplot import show
     
    # Import non-standard modules.
    import pygame
    from pygame.locals import *
    from button import Button
    from enemy_spawner import EnemySpawner
    from random import randrange
    import time
    
    pygame.init() 
    pygame.display.set_caption("消灭病毒") #The window title
    
    #Import images and define sizes
    width, height = 720, 480 
    WIZARD_WIDTH , WIZARD_HEIGHT = 78 , 103
    LIFE_WIDTH , LIFE_HEIGHT = 21 , 22 
    CARA_WIZARD_IMAGE =pygame.image.load(os.path.join('Assets','cara.png')) #character image
    
    CARA_WIZARD=pygame.transform.scale(CARA_WIZARD_IMAGE,(WIZARD_WIDTH,WIZARD_HEIGHT)) #resize
    
    BULLET_FIRE=pygame.image.load(os.path.join('Assets','fireball.png'))
    BULLET_FIRE=pygame.transform.scale(BULLET_FIRE, (28,12))
    screen = pygame.display.set_mode((width, height))
    BULLET_VEL= 7
    vel=5
    width, height = 720, 480 
    MAX_BULLET = 3
    bullets=[]
    menu=pygame.image.load(os.path.join('Assets','menu.png'))
    menu=pygame.transform.scale(menu,(width,height))
    background=pygame.image.load(os.path.join('Assets','sky.jpg')) #background image
    background=pygame.transform.scale(background, (width,height))
    gameover=pygame.image.load(os.path.join('Assets','gameover.jpg'))
    gameover=pygame.transform.scale(gameover, (width, height))
    pygame.font.init()
    pygame.mixer.init()
    fireball_sfx = pygame.mixer.Sound(os.path.join('SFX','fireball.mp3'))
    font = pygame.font.Font(os.path.join('Fonts', 'FreeSansBold.ttf'), 32)
    textX = 10
    textY = 10
    score = 0
    lounge = pygame.mixer.music.load(os.path.join('SFX','lounge.mp3'))
    
    def show_score(x,y, screen):
      
      score_text = font.render("Score :" + str(score), True, (255, 255, 255))
      screen.blit(score_text,(x,y))
      pygame.display.update()
    
    
    
    def update(wizard):
      # Go through events that are passed to the script by the window.
       for event in pygame.event.get():
            if event.type == pygame.QUIT:
              pygame.quit()
              sys.exit()
            if event.type ==pygame.KEYDOWN:
              if event.key == pygame.K_SPACE  and len(bullets) 0: #UP
        wizard.y-=vel
      if keys_pressed[pygame.K_DOWN] and wizard.y + vel + wizard.height < height : #DOWN
        wizard.y+=vel
    
    #function to handle the bullet mouvement
    def handle_bullet(bullets, wizard, enemy_spawner):
      
      for bullet in bullets:
        bullet.x += BULLET_VEL
        if wizard.colliderect(bullet):
          
    
          bullets.remove(bullet)
        elif bullet.x> width :
          bullets.remove(bullet)
    
        global score 
        for enemy in enemy_spawner.enemy_group:
          if bullet.colliderect(enemy):
            enemy.kill()
            score += 1
            pygame.mixer.Sound.play(fireball_sfx)
            bullets.remove(bullet)
    
    def get_font(size): 
      return pygame.font.Font("Fonts/font.ttf", size)
    
    
    
    def play():
      # Initialise PyGame.
      global score
    
      # Set up the clock. This will tick every frame and thus maintain a relatively constant framerate. Hopefully.
      fps = 60.0
      
      fpsClock = pygame.time.Clock()
      
      # Set up the window.
    
      
      enemy_spawner = EnemySpawner()
    
      #character
      wizard=pygame.Rect(20 ,100, WIZARD_WIDTH , WIZARD_HEIGHT )
      
      
      # Main game loop.
      dt = 1/fps # dt is the time since last frame.
      run=True
      game_over = False
      while run: # Loop forever!
    
        fpsClock.tick(fps) # You can update/draw here, I've just moved the code for neatness.
        
        if game_over == False:
          draw(screen , wizard , bullets, enemy_spawner)
          
          
          enemy_spawner.update()
          
          #handeling some events
          update(wizard)
            
          for enemy in enemy_spawner.enemy_group:
            if wizard.colliderect(enemy):
              
              game_over = True
         
          keys_pressed = pygame.key.get_pressed()
          show_score(560,40, screen)
          handle_movement(keys_pressed, wizard)
          handle_bullet(bullets,wizard, enemy_spawner)
        else:
          screen.blit(gameover,(0,0))
          pygame.display.update()
          for event in pygame.event.get():
            if event.type == pygame.QUIT:
              pygame.quit()
              sys.exit()
            if event.type == pygame.KEYDOWN:
              if event.key == pygame.K_RETURN:
                score=0
                play()
          
        
    
    def main():
      
      fps = 60.0
      fpsClock = pygame.time.Clock()
      dt = 1/fps 
      pygame.mixer.music.set_volume(0.1)
      pygame.mixer.music.play()
      
      
      while True :
        fpsClock.tick(fps)
        screen.blit(menu,(0,0))
    
        PLAY_MOUSE_POS=pygame.mouse.get_pos() # get mouse position
        PLAY_BUTTON = Button(None, pos=(width/2 -3, height/2 +51), 
                                text_input="START", font=get_font(33), base_color="#ffdd00", hovering_color="#E6A2C7")
        PLAY_BUTTON.changeColor(PLAY_MOUSE_POS)
        PLAY_BUTTON.update(screen)
        for event in pygame.event.get():
          if event.type == pygame.QUIT:
            pygame.quit() # Opposite of pygame.init
            sys.exit() 
          if event.type == pygame.MOUSEBUTTONDOWN:
            if PLAY_BUTTON.checkForInput(PLAY_MOUSE_POS):
              play()
        pygame.display.update()
     
    
    
    main()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203

    完整版代码下载地址:Python版消灭病毒、消灭新冠小游戏源代码

    Python代码大全,海量代码任你下载

  • 相关阅读:
    MHA高可用配置及故障切换
    【C++】类与对象 第二篇(构造函数,析构函数,拷贝构造,赋值重载)
    CoordinatorLayout/AppBarLayout记录滚动位置异常问题
    厦门大学马来西亚分校打造更美好的智慧校园
    OpenAI重大更新!为ChatGPT推出语音和图像交互功能
    【Ingress】
    拼多多官方开放平台接口app商品详情接口获取实时商品详情数据演示
    Sychronized和ReentrantLock的区别?
    Java真的不难(四十九)Redis的入门及使用(2)
    gitlab利用CI多工程持续构建
  • 原文地址:https://blog.csdn.net/weixin_42756970/article/details/127445176