菜鸡源码,专注精品下载!
当前位置:首页 > 建站教程 > 建站知识

编写一个使用Python开发的桌面僵尸宠物游戏代码

发布时间:2024-01-05  栏目:建站知识   浏览:   分类:python教程 Python代码 Python源码

要开发一个桌面僵尸宠物游戏,我们可以使用Python的pygame库。以下是一个简单的示例代码: ```python import pygame import sys import random # 初始化pygame pygame.init() # 设置屏幕大小 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) # 设置标题 pygame.display.set_caption("僵尸宠物") # 加载图片 bg = pygame.image.load("background.jpg") zombie = pygame.image.load("zombie.png") pet = pygame.image.load("pet.png") # 设置僵尸和宠物的初始位置 zombie_x = random.randint(0, screen_width - zombie.get_width())

python开发一个桌面行走的僵尸宠物代码,可切换僵尸皮肤,效果如下:

python开发一个桌面僵尸宠物代码
Python代码截图

python代码如下

#*_*coding:UTF-8*_*#author:Leemamas#开发时间:2021/5/280:48importsysfromPyQt5.QtGuiimport*fromPyQt5.QtCoreimport*fromPyQt5.QtWidgetsimport*classTablePet(QWidget):def__init__(self):super(TablePet,self).__init__()self.is_follow_mouse=Falseself.mouse_drag_pos=self.pos()#每隔一段时间做个动作self.timer=QTimer()self.timer.timeout.connect(self.randomAct)self.timer.start(100)##僵尸形态self.sharp=1##皇帝的新衣self.clothes=''self.clothes_key=0self.wardrobe(self.sharp)self.initUi()self.tray()##衣柜defwardrobe(self,key):default='sourceZombieZombie_'battle='sourceConeheadZombieConeheadZombie_'ifkey==0:self.clothes=default##TODO根据文件数自动获取self.clothes_key=21else:self.clothes=battleself.clothes_key=20##变身deftransformation(self):ifself.sharp==0:self.wardrobe(1)self.sharp=1else:self.wardrobe(0)self.sharp=0defrandomAct(self):#读取图片不同的地址,实现动画效果ifself.key<self.clothes_key:self.key+=1else:self.key=0self.pic_url=self.clothes+str(self.key)+'.png'self.pm=QPixmap(self.pic_url)ifnotself.is_follow_mouse:#实现行进效果ifself.w>0:self.w-=2else:self.w=1400self.move(self.w,self.h)self.lbl.setPixmap(self.pm)definitUi(self):screen=QDesktopWidget().screenGeometry()self.w=1400self.h=800self.setGeometry(self.w,self.h,300,300)#self.setWindowTitle('mypet')self.lbl=QLabel(self)self.key=0self.pic_url=self.clothes+str(self.key)+'.png'self.pm=QPixmap(self.pic_url)self.lbl.setPixmap(self.pm)#背景透明等效果self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint|Qt.SubWindow)self.setAutoFillBackground(False)self.setAttribute(Qt.WA_TranslucentBackground,True)self.show()#self.repaint()#系统托盘deftray(self):tp=QSystemTrayIcon(self)tp.setIcon(QIcon('sourceZombieZombie_0.png'))ation_quit=QAction('QUIT',self,triggered=self.quit)transformation=QAction('transformation',self,triggered=self.transformation)tpMenu=QMenu(self)tpMenu.addAction(ation_quit)tpMenu.addAction(transformation)tp.setContextMenu(tpMenu)tp.show()#鼠标事件defmousePressEvent(self,event):ifevent.button()==Qt.LeftButton:self.is_follow_mouse=Trueself.mouse_drag_pos=event.globalPos()-self.pos()event.accept()self.setCursor(QCursor(Qt.OpenHandCursor))defmouseMoveEvent(self,event):ifQt.LeftButtonandself.is_follow_mouse:self.move(event.globalPos()-self.mouse_drag_pos)xy=self.pos()self.w,self.h=xy.x(),xy.y()event.accept()defmouseReleaseEvent(self,event):self.is_follow_mouse=Falseself.setCursor(QCursor(Qt.ArrowCursor))defquit(self):self.close()sys.exit()if__name__=='__main__':app=QApplication(sys.argv)myPet=TablePet()sys.exit(app.exec_())

僵尸图片素材下载地址:https://11px.lanzoui.com/iGDvSq6jesd

评论
建站知识
建站知识
使用技巧
调试安装
运营推广