当前位置:网站首页>【Pygame小遊戲】這款“吃掉一切”遊戲簡直奇葩了?通通都吃掉嘛?(附源碼免費領)
【Pygame小遊戲】這款“吃掉一切”遊戲簡直奇葩了?通通都吃掉嘛?(附源碼免費領)
2022-06-27 15:52:00 【嗨!栗子同學】
前言
嗨!我是栗子同學。很久不見甚是思念!我回來啦~

今天給大家寫點兒什麼呢?!嘿嘿,其實這麼久沒寫代碼啦,手都不會打字了,代碼都不會敲
了,容我慢慢道來!先來點兒簡單的吧(其實是沒思路,上次寫剩下的一個無敵狙擊手的遊戲
代碼小小的改編了一下下,偷懶了哈,因為不知道寫啥了,不要打我.jpg)
等我靈感了就好好給大家寫,先練練手哈!有愛的源碼,可以評論下下次說不定就寫了撒!
今天教大家寫一款簡單的小遊戲:《吃掉一切》讓我們馬上開始叭
所有文章完整的素材+源碼都在文末公眾hao自取哦!

正文
本文是基於pygame寫的一款界面簡單的小遊戲哈!
一、運行環境
1)環境安裝 Python3、 Pycharm 、tkinter、Pygame模塊部分自帶模塊就不展示啦。
(如需安裝包、激活碼等直接 私信我即可安裝問題解答都可以的哈~)
第三方庫的安裝:pip install pygame 或者 帶鏡像源 pip install -i
https://pypi.douban.com/simple/ +模塊名2)素材(圖片:食物跟吃東西的人)

感覺食物跟人美少女不是很配來著,哈哈哈,將就一下叭,大家可以更換圖片的啦!
二、主程序
import pygame,os,random
from pygame.locals import *
from pygame.sprite import *
def load_image(name):
fullname=os.path.join(os.path.join(os.path.split(os.path.abspath(__file__))[0],"filedata"),name)
image=pygame.image.load(fullname)
return image
def load_sound(name):
fullname=os.path.join(os.path.join(os.path.split(os.path.abspath(__file__))[0],"filedata"),name)
sound=pygame.mixer.Sound(fullname)
return sound
class Tip(Sprite):
def __init__(self,screen,fontrender,waitticks,pos):
super(Tip,self).__init__()
self.screen=screen
self.image=fontrender
self.rect=self.image.get_rect()
self.rates=0
self.waitticks=waitticks
self.rect.center=pos
def update(self):
self.rates+=1
if self.rates>=self.waitticks:
self.kill()
class Surface:
def __init__(self,screen):
self.screen=screen
self.image=load_image("eatingface.png")
self.rect=self.image.get_rect()
self.rect.center=self.screen.get_rect().center
self.speed=3.7
self.caneat=20
self.eat=self.caneat
self.moveUp=False
self.moveDown=False
self.moveLeft=False
self.moveRight=False
self.faceatleft=False
self.punch=0
def update(self):
if self.punch==0:
if self.moveUp and self.rect.top>0:
self.rect.centery-=self.speed
if self.moveDown and self.rect.bottom<HEIGHT:
self.rect.centery+=self.speed
if self.moveLeft and self.rect.left>0:
if not self.faceatleft:
self.faceatleft=True
self.image=pygame.transform.flip(self.image,True,False)
self.rect.centerx-=self.speed
if self.moveRight and self.rect.right<WIDTH:
if self.faceatleft:
self.faceatleft=False
self.image=pygame.transform.flip(self.image,True,False)
self.rect.centerx+=self.speed
else:
self.punched()
def blit(self):
self.screen.blit(self.image,self.rect)
def punched(self):
self.punch+=1
if self.punch>60:
self.punch=0
def eats(self,num):
self.eat+=num
if self.eat>=100:
self.eat=100
return "True"
elif self.eat<=0:
self.eat=0
return "False"
return "None"
def reset(self):
self.image=load_image("eatingface.png")
self.rect=self.image.get_rect()
self.rect.center=self.screen.get_rect().center
self.speed=3.7
self.eat=self.caneat
self.moveUp=False
self.moveDown=False
self.moveLeft=False
self.moveRight=False
self.faceatleft=False
self.punch=0
class Food(Sprite):
def __init__(self,screen,surface,tips,gameFont):
super(Food,self).__init__()
self.screen=screen
self.surface=surface
self.tips=tips
self.gameFont=gameFont
self.screenrect=self.screen.get_rect()
self.image=load_image("eatingfood.png")
self.rect=self.image.get_rect()
self.rectat=random.choice(["top","left","right","bottom"])
self.xspeed=round(random.uniform(1,2),2)
self.yspeed=round(random.uniform(1,2),2)
if self.rectat=="top":
self.rect.center=(random.randint(0,WIDTH),0)
elif self.rectat=="bottom":
self.rect.center=(random.randint(0,WIDTH),HEIGHT)
self.yspeed=-self.yspeed
elif self.rectat=="left":
self.rect.center=(0,random.randint(0,HEIGHT))
elif self.rectat=="right":
self.xspeed=-self.xspeed
self.rect.center=(WIDTH,random.randint(0,HEIGHT))
def update(self):
global toohungry,isfull
if self.surface.faceatleft:
if self.rect.left<self.surface.rect.left<=self.rect.right:
if self.surface.rect.top<self.rect.top<self.surface.rect.bottom or self.surface.rect.bottom>self.rect.bottom>self.surface.rect.top:
self.kill()
if self.surface.eats(2)=="True":
isfull=True
return
else:
if self.rect.right>self.surface.rect.right>=self.rect.left:
if self.surface.rect.top<self.rect.top<self.surface.rect.bottom or self.surface.rect.bottom>self.rect.bottom>self.surface.rect.top:
self.kill()
if self.surface.eats(2)=="True":
isfull=True
return
if collide_rect(self,self.surface):
self.surface.punched()
if self.surface.eats(-1)=="False":
toohungry=True
return
self.tips.add(Tip(self.screen,self.gameFont.render("Dizzy!",True,(255,255,255)),
60,self.surface.rect.center))
self.away()
self.rect.centerx+=self.xspeed
self.rect.centery+=self.yspeed
if self.rect.top>self.screenrect.height or self.rect.bottom<0:
self.kill()
elif self.rect.left>self.screenrect.width or self.rect.right<0:
self.kill()
def away(self):
self.xspeed=-self.xspeed
self.yspeed=-self.yspeed
WIDTH=700
HEIGHT=600
toohungry=False
isfull=False
def initmain():
pygame.init()
screen=pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Eater")
gameFont=pygame.font.SysFont("宋體",26,True)
fpstime=pygame.time.Clock()
surface=Surface(screen)
foods=Group()
tips=Group()
def mainit():
global toohungry,isfull
foods.empty()
tips.empty()
surface.reset()
rates=0
toohungry=False
isfull=False
while ((not isfull) and (not toohungry)):
fpstime.tick(100)
screen.fill((0,255,0))
screen.blit(gameFont.render("Full "+str(surface.eat)+"%",True,(0,0,0)),(2,2))
rates+=1
if rates%50==0:
foods.add(Food(screen,surface,tips,gameFont))
foods.draw(screen)
foods.update()
surface.blit()
surface.update()
tips.draw(screen)
tips.update()
for event in pygame.event.get():
if event.type==QUIT:
toohungry=True
isfull=True
elif event.type==KEYDOWN:
if event.key==K_RIGHT:
surface.moveRight=True
elif event.key==K_LEFT:
surface.moveLeft=True
elif event.key==K_UP:
surface.moveUp=True
elif event.key==K_DOWN:
surface.moveDown=True
elif event.key==K_SPACE:
surface.speed=5
elif event.type==KEYUP:
if event.key==K_RIGHT:
surface.moveRight=False
elif event.key==K_LEFT:
surface.moveLeft=False
elif event.key==K_UP:
surface.moveUp=False
elif event.key==K_DOWN:
surface.moveDown=False
elif event.key==K_SPACE:
surface.speed=3.5
pygame.display.flip()
notbreak=True
while notbreak:
screen.fill((0,255,0))
if toohungry and isfull:
screen.blit(gameFont.render("Esc To Exit!",True,(128,128,128)),(2,2))
elif toohungry:
screen.blit(gameFont.render("Too hungry!",True,(0,0,0)),(2,2))
elif isfull:
screen.blit(gameFont.render("Full!",True,(0,0,0)),(2,2))
screen.blit(gameFont.render("Space To Retry",True,(128,128,128)),(2,32))
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
__import__("sys").exit()
elif event.type==KEYUP:
if event.key==K_ESCAPE:
pygame.quit()
__import__("sys").exit()
elif event.key==K_SPACE:
notbreak=False
pygame.display.flip()
mainit()
mainit()
if __name__=="__main__":
initmain()總結
嗨,效果挺簡單滴啦,我就不展示了就一張圖,鼠標左鍵一直移動吃掉掉落的食物即可啦!
免費的源碼基地——
你們的支持是我最大的動力!!記得三連哦~mua 歡迎大家閱讀往期的文章哦~
往期部分文章推薦——
項目1.5 Pygame小遊戲:植物大戰僵屍遊戲真的有“毒”?戒不掉啊~
項目1.6 【Pygame小遊戲】鬥地主我見多了,BUT 這款開源歡樂鬥地主,最讓人服氣~
項目1.7 【Pygame小遊戲】神還原【歡樂無窮的雙人坦克大戰】小程序遊戲,上手開玩~
項目3.1 【Pygame實戰】如果你是賽車愛好者:這款新賽車遊戲分分鐘讓你上癮(超跑又是誰的夢想?)
項目3.2 【Pygame小遊戲】炸裂全場、超級炸彈人“爆炸”登場,這是你的童年嘛?
文章匯總——
項目1.0 Python—2021 |已有文章匯總 | 持續更新,直接看這篇就够了
(更多內容+源碼都在文章匯總哦!!歡迎閱讀~)
文章匯總——
匯總: Python文章合集 | (入門到實戰、遊戲、Turtle、案例等)
(文章匯總還有更多你案例等你來學習啦~源碼找我即可免費!)

边栏推荐
- Teach you how to package and release the mofish Library
- Beginner level Luogu 1 [sequence structure] problem list solution
- Scrapy framework (I): basic use
- Eolink launched a support program for small and medium-sized enterprises and start-ups to empower enterprises!
- The role of the symbol @ in MySQL
- Gin general logging Middleware
- Knowledge map model
- Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
- [issue 17] golang's one-year experience in developing Meitu
- 避孕套巨头过去两年销量下降40% ,下降原因是什么?
猜你喜欢

关于TensorFlow使用GPU加速

The latest development course of grain college in 2022: 8 - foreground login function

Derivation of Halcon camera calibration principle

Introduction to TTCAN brick moving
![Luogu_ P1008 [noip1998 popularization group] triple strike_ enumeration](/img/9f/64b0b83211bd1c615f2db9273bb905.png)
Luogu_ P1008 [noip1998 popularization group] triple strike_ enumeration

Weekly snapshot of substrate technology 20220411

LeetCode每日一练(无重复字符的最长子串)

【kotlin】第二天

SQL injection principle

PSS:你距离NMS-free+提点只有两个卷积层 | 2021论文
随机推荐
Pisa-Proxy 之 SQL 解析实践
Design of CAN bus controller based on FPGA (with main codes)
洛谷_P1008 [NOIP1998 普及组] 三连击_枚举
请问阿里云实验中 k8s 对于3306端口转发,同时开启mysql客户端就会异常终止,是什么原因呢?
Fundamentals of software engineering (I)
PSS:你距离NMS-free+提点只有两个卷积层 | 2021论文
正则匹配以什么开头、以什么结尾,以非什么开头,以非什么结尾
[kotlin] the next day
Eolink 推出面向中小企业及初创企业支持计划,为企业赋能!
28 object method extension
【Pygame小游戏】这款“吃掉一切”游戏简直奇葩了?通通都吃掉嘛?(附源码免费领)
我想买固收+产品,但是不了解它主要投资哪些方面,有人知道吗?
ICML 2022 ぷ the latest fedformer of the Dharma Institute of Afghanistan ⻓ surpasses SOTA in the whole process of time series prediction
固收+产品有什么特点?
Let's talk about the process of ES Indexing Documents
SQL parsing practice of Pisa proxy
Piblup test report 1- pedigree based animal model
If you want to use DMS to handle database permissions, can you only use Alibaba cloud ram accounts (Alibaba cloud RDS)
Use of abortcontroller
Luogu_ P1002 [noip2002 popularization group] crossing the river_ dp