0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看威廉希尔官方网站 视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

Python版蚊子大作战源代码

汽车电子威廉希尔官方网站 来源:Python代码大全 作者: Python代码狂人 2023-02-24 10:29 次阅读

Python灭蚊小游戏源代码,超解压的灭蚊小游戏,通过消灭蚊子赚钱,屏幕里的蚊子不被消灭就会被蚊子吸血,通过商店购买血包、血瓶、血桶、回血针来使自己回血,也可以在商店购买不同的灭蚊工具:高级灭蚊拍、灭蚊喷课剂、灭蚊灯、提高房间温度等来提高灭蚊效率,可对游戏音效、全屏、语言进行设置。

游戏玩法,移动鼠标,找准蚊子单击左键将其消灭。游戏中按Esc键来选择使用不同的道具,游戏中先按Esc键再按空格键即可进入主菜单页面(可由此进入商店),进行游戏道具的购买。

直接运行main.py即可。

完整Python版蚊子大作战源代码下载地址:

https://download.csdn.net/download/weixin_42756970/86794102

游戏主菜单页面:

poYBAGP4IHOAO4pqAANtaCO_CJY454.png

游戏设置

poYBAGP4IH2AWM-kAAOy5wYSlQg505.png

游戏商店

pYYBAGP4IIyAA7NrAAI59jiwRNk123.png

游戏道具选择

pYYBAGP4IJqAMrY6AAQJTG9fHck712.png

游戏

pYYBAGP4IKWAWX3qAANsDIxxYXo182.png

核心程序代码

ort os
import tkinter.messagebox

import nathlib as nlib
import sys
import time

version_name = "snapshot_015"  # DO NOT FORGET TO CHANGE FILEMANAGER CONSTANTS !!!
version_number = 1

debug_mouse = False

if os.path.isfile("latest.log"):
    try:
        os.remove('latest.log')
    except Exception as e:
        nlib.log("Couldn't remove latest.log : {}".format(str(e)), "warn")
if not os.path.isfile("enable_logs.txt"):
    with open("enable_logs.txt", "w") as file:
        file.write("# Enable logging ? "yes" or "no"nDefaults to "no"to avoid unnecessary log file rewriting.n"
                   "You should let this parameter to "no" unless you have an error.nno")
enable_logs = "latest.log"
with open("enable_logs.txt", "r") as file:
    if file.readlines()[3].startswith("no"):
        enable_logs = None

nlib.start_logs(enable_logs)
nlib.log("Launching game version {0} ...".format(version_name), "info")


def exception_handler(type, value, traceback):
    nlib.log("{0}: {1}".format(repr(value).split("(")[0], value), "critical", 'main')
    from tkinter import Tk, messagebox
    root = Tk()
    root.attributes("-topmost", True)
    root.withdraw()
    messagebox.showerror("An error occurred", "{}: {}".format(repr(value).split("(")[0], value))
    root.destroy()
    sys.exit()


enable_exception_handler = False
if enable_exception_handler:
    sys.excepthook = exception_handler
enable_hash_checking = False  # Should only be enabled when released !!!

if enable_hash_checking:
    import hashlib

    hashes = [["LICENSE", "d5dc6d638156797c63fffd4bc908a3ec380e37d051996284736c6222438f3c9a"],
              ["nathlib.py", "e85f2234f4a8907d56f73a984a39b6fbcb8aa54e5bc82b62f11ad684eed83fa3"],
              ["README.MD", "70c728ac19b13ff9a343743ee5cf821d8dfea5d253201efbabbfc284d3951702"],
              ["settings_window.py", "17ca489a5fea4fe8243f6c6a4eaeaae8a004e9e10c0516bb4889688d7f02ecf2"],
              ["scripts/util/FileManager.py", "1c2c2e18c473429a0d3c1ee607adc1055eed3efe64bb5b1776b0eaff9acae0a3"],
              ["scripts/util/default/lang/en_US.py", "e32c190196fcbb4e55d07e5bc99d9f665fd9b7f9bf1dd98d72ffd04f2d0481c1"]
        ,
              ["scripts/util/default/lang/fr_FR.py", "b105710bdec2b292ebb3b8fa8782601c3786c1f3b9cb4a9d9410926d2dce280c"]
              ]

    for _hash in hashes:

        sha256_hash = hashlib.sha256()
        with open(_hash[0], "rb") as f:
            # Read and update hash string value in blocks of 4K
            for byte_block in iter(lambda: f.read(4096), b""):
                sha256_hash.update(byte_block)
            if sha256_hash.hexdigest() == _hash[1]:
                nlib.log("Successfully checked hash for '{0}'".format(_hash[0]), "info")
            else:
                nlib.log("File '{0}' got a different hash than expected !nExpected '{1}',nGot '{2}' !"
                         .format(_hash[0], _hash[1], sha256_hash.hexdigest()), "fatal")
                time.sleep(1)
                sys.exit()
else:
    nlib.log("Skipping hash checking !", "warn")

# Importation des modules

# try:
print("------------------------------Pygame--Details--------------------------")
import pygame

print("-----------------------------------------------------------------------")
import time
import random
import math
import tkinter as tk
from tkinter import simpledialog
import pygame.gfxdraw  # necessary as pygame doesn't load it by default !
from settings_window import open_settings
import ptext
import operator

# except ImportError:
# print("[ERROR]: Failed to import modules !")
# from sys import exit

# exit()

try:
    from scripts.util.FileManager import *

    for x in range(0, lang_number):
        exec("from scripts.util.FileManager import " + lang_files_to_load[x])
except:
    nlib.log("Failed to load resources, aborting ...", "critical")
    sys.exit()

# Initialisation de Pygame

pygame.init()
pygame.font.init()
if settings_list[2]:
    pygame.mixer.init()  # Sons de pygame.

# Definition de la fenêtre

window_x = 1280
window_y = 720
if not settings_list[3]:
    screen = pygame.display.set_mode((window_x, window_y))
else:
    screen = pygame.display.set_mode((window_x, window_y), pygame.FULLSCREEN | pygame.SCALED)
pygame.display.set_caption("NoMoskito!")
pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_ARROW)
clock = pygame.time.Clock()
FPS = 60  # yeah, really !

# ressources à charger
shop_blood_bag = pygame.image.load("resources/blood_bag.png").convert_alpha()
shop_blood_bag = pygame.transform.scale(shop_blood_bag, (50, 50))
shop_blood_bottle = pygame.image.load("resources/blood_bottle.png").convert_alpha()
shop_blood_bottle = pygame.transform.scale(shop_blood_bottle, (80, 80))
shop_blood_barrel = pygame.image.load("resources/barrel_blood.png").convert_alpha()
shop_blood_barrel = pygame.transform.scale(shop_blood_barrel, (96, 96))
shop_blood_infusion = pygame.image.load("resources/blood_infusion.png").convert_alpha()
shop_blood_infusion = pygame.transform.scale(shop_blood_infusion, (176, 117))
swatter_pro = pygame.image.load("resources/swatter_pro.png").convert_alpha()
shop_swatter_pro = pygame.transform.scale(swatter_pro, (41, 275))
bzio_ruler = pygame.image.load("resources/imposant_ruler.png").convert_alpha()
shop_bzio_ruler = pygame.transform.scale(bzio_ruler, (128, 128))
pause_bzio_ruler = pygame.transform.rotate(shop_bzio_ruler, 90)
img_heat_wave = pygame.image.load("resources/hot.png").convert_alpha()
shop_heat_wave = pygame.transform.scale(img_heat_wave, (17, 48))
img_spray = pygame.image.load("resources/anti_moskito_spray.png").convert_alpha()
shop_spray = pygame.transform.scale(img_spray, (38, 85))
img_lamp = pygame.image.load("resources/anti_moskito_lamp.png").convert_alpha()
shop_lamp = pygame.transform.scale(img_lamp, (66, 95))
img_wait_bar_1 = pygame.image.load("resources/resource_0.png").convert_alpha()
img_icon = pygame.image.load("resources/resource_2.png").convert_alpha()
img_background = pygame.image.load("resources/resource_1.jpg").convert()
img_logo = pygame.image.load("resources/resource_4.png").convert_alpha()
img_btn_normal = pygame.image.load("resources/resource_5.png").convert()
img_btn_hovered = pygame.image.load("resources/resource_6.png").convert()
img_swatter_1 = pygame.image.load("resources/resource_8.png").convert_alpha()
pause_swatter_1 = pygame.transform.scale(img_swatter_1, (41, 275))
dark_img = pygame.image.load("resources/dark.png").convert_alpha()
init_img = pygame.image.load("resources/init.png").convert()
img_pix_wait_bar = pygame.Surface((1, 13))
img_pix_wait_bar.fill((104, 255, 4))
img_blood_bar = pygame.image.load("resources/resource_9.png").convert_alpha()
btn_font = pygame.font.Font("resources\ComicSansMSM.ttf", 30)
img_pix_blood_bar = pygame.Surface((38, 1))
img_pix_blood_bar.fill((255, 0, 0))
img_moskito_list = [pygame.image.load("resources/mosquito_1.png").convert_alpha(),
                    pygame.image.load("resources/mosquito_2.png").convert_alpha(),
                    pygame.image.load("resources/mosquito_3.png").convert_alpha(),
                    pygame.image.load("resources/mosquito_4.png").convert_alpha()]

shop_bg = pygame.Surface((1180, 660))
pygame.gfxdraw.rectangle(shop_bg, shop_bg.get_rect(), (206, 237, 31))
pygame.gfxdraw.rectangle(shop_bg, pygame.rect.Rect(1, 1, 1178, 658), (206, 237, 31))
pygame.gfxdraw.rectangle(shop_bg, pygame.rect.Rect(2, 2, 1176, 656), (206, 237, 31))
pygame.gfxdraw.box(shop_bg, pygame.rect.Rect(3, 3, 1174, 654), (235, 248, 165))
pygame.gfxdraw.box(shop_bg, pygame.rect.Rect(3, 3, 1174, 50), (220, 242, 96))
pause_bg = pygame.Surface((1280, 720))

shop_btn_ids = {"shop_btn_blood_2": 0,
                "shop_btn_blood_3": 1,
                "shop_btn_blood_4": 2,
                "shop_btn_blood_5": 3,
                "shop_btn_heat_wave": 6,
                "shop_btn_spray": 7,
                "shop_btn_lamp": 8}

sounds_moskitos_list = ["resources/sounds/Single_moskito_1.wav",
                        "resources/sounds/Single_moskito_2.wav",
                        "resources/sounds/Single_moskito_3.wav"]

img_tmp = pygame.Surface((4, 4))
img_tmp.fill((255, 255, 255))
font_a = pygame.font.Font("resources\ComicSansMSM.ttf", 70)

correction_angle = 90

opaque = 1055

spray_sound_obj = pygame.mixer.Sound("resources/sounds/Single_moskito_4.wav")#

pygame.display.set_icon(img_icon)

sprite_group = pygame.sprite.Group()

anti_moskito_spray = 0
moskito_lamp_time = 0

isMenu = True

pause_btn_list = []

# fichier lang

lang = lang_files_names[settings_list[0]]
default_lang = eval(lang)

play_btn_text = btn_font.render(default_lang[0], True, (153, 153, 0))
settings_btn_text = btn_font.render(default_lang[1], True, (153, 153, 0))
font_a_text = font_a.render(default_lang[11], True, (153, 153, 0))


def get_bziocoins():
    return get_better_score()[2]


def get_inventory():
    a = get_better_score()
    return [a[4], a[3]]


def remove_item_from_inventory(inv_list, item_id: int):  # remove 1 item and return the result
    tmp_list = []
    found = False
    for _i in inv_list:
        tmp_list.append(_i)  # simple way to copy two lists in python :/
    for item in range(0, len(inv_list)):
        if inv_list[item] == item_id:
            if not found:
                tmp_list.pop(item)
                found = True
    return tmp_list


def save_bziocoins(bziocoins):
    a = get_better_score()
    overwrite_better_score(a[0], a[1], bziocoins, a[3], a[4])


def save_inventory(inv, buy):  # buy = which swatter you bought.
    a = get_better_score()
    overwrite_better_score(a[0], a[1], a[2], buy, inv)


# definition du joueur
BLOOD = 600
blood_infusion = 0
blood_infusion_total = 0
blood_factor = 0

shop_hovered_id = -1


class Var:
    def __init__(self):
        super(Var, self).__init__()
        self.inventory = []
        self.change_fullscreen = False
        self.bziocoins = get_bziocoins()
        self.last_mouse = (0, 0)
        self.is_settings_to_save = False
        self.IsGamePaused = False
        self.is_update_checked = False
        self.isMenu = True
        self.can_click = True
        self.click_delay = 388
        self.click_rate = 1
        self.blood = BLOOD
        self.moskitos_killed = 0
        self.Playing = False
        self.Final_Menu = False
        self.chrono = 0  # ms
        self.latest_chrono = 0
        self.renew_sound = True
        self.enable_sound = settings_list[2]
        self.btn_hover_list = []
        self.btn_click_list = []
        self.Final_verdict = False
        self.shop = False
        self.best_score = [0, "nobody"]

    def set_value(self, var_name, var_value):
        if var_name == "is_settings_to_save":
            self.is_settings_to_save = var_value
        if var_name == "is_update_checked":
            self.is_update_checked = var_value

    def get_value(self, var_name):
        return eval("self." + var_name)


global_var = Var()
global_var.inventory = get_inventory()[0]


# class Player(pygame.sprite.Sprite):
#     def __init__(self):
#         super().__init__()
#         self.image = img_spaceship
#         self.rect = self.image.get_rect()  # Adapte le taille du personnage a la taille de l'image.
#         self.velocity = [0, 0]
#         self.rect.x = int(window_x / 2.15)
#         self.rect.y = int(window_y / 2.15)


# class Star(pygame.sprite.Sprite):
#     def __init__(self):
#         super().__init__()
#         self.image = img_star
#         self.rect = self.image.get_rect()  # Adapte le taille du personnage a la taille de l'image.
#         self.velocity = [0, 0]
#         self.rect.x = random.randint(0, window_x)
#         self.rect.y = random.randint(0, window_y)
#
#     def walk_with_degrees(self, degree, distance):
#         self.rect.y = int(self.rect.y + (distance * sin(degree)))
#         self.rect.x = int(self.rect.x + (distance * cos(degree)))
#
#     def update(self, angle_in):
#         self.walk_with_degrees(angle_in, 1)
FPS_COUNT = 0


class MoskitoSpawnHandler:
    def __init__(self):
        super(MoskitoSpawnHandler, self).__init__()
        self.time_spent = 0
        self.time_limit = 600
        self.moskito_list = []

    def update(self):
        self.time_spent = self.time_spent + 4
        _tmp = 1
        if global_var.blood < 295:
            _tmp = 2
        if global_var.blood < 196:
            _tmp = 3
        if global_var.blood < 147:
            _tmp = 4
        if self.time_spent > self.time_limit:
            self.time_spent = 0
            for moskito in range(0, random.randint(1, _tmp)):
                self.moskito_list.append(Moskito())
            self.time_limit = self.time_limit - (self.time_limit / 85)
        for msk in self.moskito_list:
            msk.update()

完整Python版蚊子大作战源代码下载地址:

https://download.csdn.net/download/weixin_42756970/86794102

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 源代码
    +关注

    关注

    96

    文章

    2945

    浏览量

    66731
  • 设置
    +关注

    关注

    0

    文章

    16

    浏览量

    14739
  • python
    +关注

    关注

    56

    文章

    4793

    浏览量

    84631
收藏 人收藏

    评论

    相关推荐

    开学大作战!元气加满不做卢瑟!!

    自己要改的缺点向好友公开,总不会厚脸皮不行动吧!只需要登录人人网找到“惠普电脑”即可啦!努力了才会成为下一个乔布斯!才能成为下一个盖茨巴菲特!才会成为下一个林书豪!更何况,这样才会有姑娘喜欢!想要在新学期脱卝光的,想要在考卝试时轻卝松过的,想要在年轻时不做卢瑟的!新学期大作战,赶紧走起来!
    发表于 02-24 16:12

    电子发烧友网电子杂志11月刊新鲜出炉:嵌入式厂商大作战

    `电子发烧友网电子杂志11月刊新鲜出炉:嵌入式厂商大作战!——内容精彩纷呈,绝对不容错过!`
    发表于 01-08 16:47

    移动电竞开发探索的新生期:《球球大作战》的移动电竞探索

    两年前《球球大作战》悄无声息地出现在苹果商店,也许连制作团队自己也没有想到,这款看上去与世无争的“球吃球”游戏会有在上海梅赛德斯-奔驰文化中心举办大型比赛的一天。从简单的“球吃球”模式到台上台下数千名现场观众为选手们绷紧神经,它只用了不到一年的时间。
    的头像 发表于 02-17 18:27 2328次阅读
    移动电竞开发探索的新生期:《球球<b class='flag-5'>大作战</b>》的移动电竞探索

    Python微服务开发的源代码合集免费下载

    本文档的主要内容详细介绍的是Python微服务开发的源代码合集免费下载。
    发表于 09-20 08:00 3次下载

    Python深度学习2018的源代码合集免费下载

    本文档的主要内容详细介绍的是Python深度学习2018的源代码合集免费下载。
    发表于 01-16 10:25 70次下载

    python实现目标检测的源代码免费下载

    本文档的主要内容详细介绍的是python实现目标检测的源代码免费下载
    发表于 04-09 08:00 6次下载
    <b class='flag-5'>python</b>实现目标检测的<b class='flag-5'>源代码</b>免费下载

    python文件读取的源代码免费下载

    本文档的主要内容详细介绍的是python文件读取的源代码免费下载。
    发表于 08-07 17:14 20次下载
    <b class='flag-5'>python</b>文件读取的<b class='flag-5'>源代码</b>免费下载

    使用Python按行读文件的源代码免费下载

    本文档的主要内容详细介绍的是使用Python按行读文件的源代码免费下载。
    发表于 10-22 17:57 12次下载
    使用<b class='flag-5'>Python</b>按行读文件的<b class='flag-5'>源代码</b>免费下载

    如何用C语言实现球球大作战项目

      这篇文章主要为大家详细介绍了 C语言实现——《球球大作战项目》 ,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下! 在室友面前秀一下岂不是美滋滋!   游戏介绍
    的头像 发表于 11-21 16:36 2053次阅读

    智慧家庭系列文章 | 控制权大作战:谁才是智能家居系统的控制中心?

    智慧家庭系列文章 | 控制权大作战:谁才是智能家居系统的控制中心?
    发表于 10-31 08:23 0次下载
    智慧家庭系列文章 | 控制权<b class='flag-5'>大作战</b>:谁才是智能家居系统的控制中心?

    C/C++实现球球大作战(高仿版),全源码分享带你轻松完成!

    《球球大作战》虽然在玩法上类似于大球吃小球的模式看起来很单薄。但是在游戏过程中会出现无数种意外情况,这就需要玩家运用一系列策略来达到不被吃和吃掉别人球的目的,大大增加了游戏的耐玩性。
    的头像 发表于 02-02 09:40 2686次阅读

    Python版警察抓小偷游戏源代码

    Python版警察抓小偷游戏源代码,有多个难度级别,直接运行game.py,输入难度级别(1-13)。不同的难度等级对应不同的图形。
    的头像 发表于 02-24 09:56 1709次阅读
    <b class='flag-5'>Python</b>版警察抓小偷游戏<b class='flag-5'>源代码</b>

    Python版超市管理系统源代码

    Python版超市管理系统源代码,基于django+mysql安装步骤
    的头像 发表于 02-24 09:59 1698次阅读
    <b class='flag-5'>Python</b>版超市管理系统<b class='flag-5'>源代码</b>

    Python编程实战(源代码)

    [源代码]Python编程实战 妙趣横生的项目之旅
    发表于 06-06 17:49 3次下载

    [源代码]Python算法详解

    [源代码]Python算法详解[源代码]Python算法详解
    发表于 06-06 17:50 0次下载