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 timepygame.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 imageCARA_WIZARD=pygame.transform.scale(CARA_WIZARD_IMAGE,(WIZARD_WIDTH,WIZARD_HEIGHT)) #resizeBULLET_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)<MAX_BULLET : # event to shot bulletsbullet= pygame.Rect(wizard.x + wizard.width , wizard.y + wizard.height//2 , 10 , 5)bullets.append(bullet)def draw(screen , wizard,bullets, enemy_spawner):screen.fill((0, 0, 0)) # Fill the screen with black.screen.blit(background,(0,0))# Redraw screen here.screen.blit(CARA_WIZARD , (wizard.x , wizard.y)) # display characterenemy_spawner.enemy_group.draw(screen)for bullet in bullets :screen.blit(BULLET_FIRE, bullet ) #display bullets # Flip the display so that the things we drew actually show up.pygame.display.flip()# function to handle the character mouvements (Up,Down)
def handle_movement(keys_pressed, wizard):if keys_pressed[pygame.K_UP] and wizard.y - vel > 0: #UPwizard.y-=velif keys_pressed[pygame.K_DOWN] and wizard.y + vel + wizard.height < height : #DOWNwizard.y+=vel#function to handle the bullet mouvement
def handle_bullet(bullets, wizard, enemy_spawner):for bullet in bullets:bullet.x += BULLET_VELif 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 += 1pygame.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.0fpsClock = pygame.time.Clock()# Set up the window.enemy_spawner = EnemySpawner()#characterwizard=pygame.Rect(20 ,100, WIZARD_WIDTH , WIZARD_HEIGHT )# Main game loop.dt = 1/fps # dt is the time since last frame.run=Truegame_over = Falsewhile 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 eventsupdate(wizard)for enemy in enemy_spawner.enemy_group:if wizard.colliderect(enemy):game_over = Truekeys_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=0play()def main():fps = 60.0fpsClock = 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 positionPLAY_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.initsys.exit() if event.type == pygame.MOUSEBUTTONDOWN:if PLAY_BUTTON.checkForInput(PLAY_MOUSE_POS):play()pygame.display.update()main()

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

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

Python版消灭病毒、消灭新冠小游戏源代码相关推荐

  1. Python是男人就下一百层小游戏源代码

    是男人就下一百层小游戏Python小游戏源程序,共包含两个程序文件:game.py和是男人就下一百层.py.程序运行截图如下: 更多Python源代码,请微信关注:Python代码大全, game.p ...

  2. python手机版做小游戏代码大全-Python大牛手把手教你做一个小游戏,萌新福利!...

    原标题:Python大牛手把手教你做一个小游戏,萌新福利! 引言 最近python语言大火,除了在科学计算领域python有用武之地之外,在游戏.后台等方面,python也大放异彩,本篇博文将按照正规 ...

  3. 微信消灭病毒哪个服务器好,消灭病毒怎么玩 微信小程序98k攻略

    最近微信上面的小程序消灭病毒98k十分火爆,是比特小队制作团队出的一款游戏,吸引了很多玩家,那么消灭病毒怎么玩呢?来随小编看看吧. 1 第一步:打开微信,搜索框内搜索"消灭病毒", ...

  4. Python基于改进Unet的新冠肺炎等级分割系统(源码&教程)

    1.研究背景 新冠肺炎给人类带来极大威胁,自动精确分割新冠肺炎CT图像感染区域可以辅助医生进行诊断治疗,但新冠肺炎的弥漫性感染.感染区域形状多变.与其他肺部组织极易混淆等给CT图像分割带来挑战.为此, ...

  5. Python版基于pygame的玛丽快跑小游戏源代码,玛丽冒险小游戏代码,支持双人模式

    基于pygame的玛丽快跑小游戏源代码,玛丽冒险小游戏代码,支持双人模式 按空格进入单人模式,按't'进入双人模式,双人模式下玛丽1采用空格键上跳,玛丽2采用方向上键上跳. 完整代码下载地址:Pyth ...

  6. Python版跳跳方块小游戏源代码,跳跳益智游戏代码

    Python版跳跳方块小游戏源代码,跳跳益智游戏代码,通过方向键的左右键来控制方块向左上还右上跳跃,按R键重要开始游戏.相当有难度,快来挑战你自己吧 jump_square.py # import o ...

  7. Python版见缝插针小游戏源代码,球球旋转大作战源程序

    见缝插针游戏是一款非常考验玩家手眼协调能力的休闲益智虐心虐脑小游戏,玩法很简单,但要过关却很有挑战性哟! 主要是将一系列的小球,插入到旋转的摩天轮转盘当中,插入过程中不能碰到旋转的摩天轮上的其他小球, ...

  8. 基于Python pygame简易版斗兽棋小游戏源代码

    基于Python pygame简易版斗兽棋小游戏源代码 游戏规则如下: 胜利条件: 1.吃掉对方全部棋子 2.走入对方兽穴(不可进入自己洞穴) 吃法: 1.象>狮>虎>豹>狼& ...

  9. 视频教程-微信小程序系统教程python版[3/3阶段]_微信小程序支付-手游开发

    微信小程序系统教程python版[3/3阶段]_微信小程序支付 微信企业号星级会员.10多年软件从业经历,国家级软件项目负责人,主要从事软件研发.软件企业员工技能培训.已经取得计算机技术与软件资格考试 ...

最新文章

  1. 蚂蚁金服天街:OceanBase 在大促 5 年来的技术演进
  2. 【leetcode】148. Sort List
  3. Eclipse中的插件安装
  4. php 5w的并发需要多少台服务器_php使用异步编程是怎样的?
  5. Gym - 101972A Multiplication Dilemma(模拟)
  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:表格单元格使用了 bg-primary 类...
  7. labelImg重新开启工具箱(栏)
  8. kali ip查询_UDP/IP硬件协议栈设计(一):缘起
  9. python入门——P43魔法方法:算数运算2
  10. Flex 加载pdf
  11. k短路-洛谷P2483 [SDOI2010]魔法猪学院
  12. .第一天.net 学习理论
  13. C#笔记30:Trace、Debug和TraceSource的使用以及日志设计
  14. 车机没有carlife可以自己下载吗_视频实测:苹果CarPlay和百度CarLife到底哪个更好用...
  15. nginx 客户端返回499的错误码
  16. 使用opencv将16位深度图转灰度图
  17. java web 打印 Cl0dup
  18. 针对云主机卡死问题的定位分析方法
  19. 现在爆火的数字孪生城市,到底是什么技术?
  20. Linux网络之连接跟踪(conntrack)

热门文章

  1. java 时区 edt_时区EST与EST5EDT有何不同?
  2. QT5 Qbs编译系统下添加打印机支持模块
  3. komodo ide php,Komodo IDE
  4. 员工脉动/脉搏调查:它们是什么以及它们为何如此重要?
  5. this以及super
  6. 安卓开源项目周报0308
  7. 管道爬行机器人内部陀螺仪_油管内壁爬行机器人的机械结构设计
  8. JavaScript for impatient programmers
  9. php日历天气预报下载安装手机桌面_怎样设置我手机上的时间和天气预报放到桌面上?...
  10. Java 的几把 JVM 级锁