当前位置:网站首页>[pyGame games] this "eat everything" game is really wonderful? Eat them all? (with source code for free)
[pyGame games] this "eat everything" game is really wonderful? Eat them all? (with source code for free)
2022-06-27 15:54:00 【Hi! Lizi】
Preface
hi ! I'm classmate chestnut . Long time no see is missing ! I've come back ~

What can I write for you today ?! Hey , Actually, I haven't written code for so long , I can't type with my hands , Code doesn't even knock
了 , Let me take my time ! Let's start with something simple ( Actually, I have no idea , Last time I wrote the remaining game of an invincible sniper
The code is slightly adapted , Lazy ha , Because I don't know what to write , Don't hit me .jpg)
When I get inspired, I will write for you , Practice your hands first ! Love source code , You can comment that you may write SA next time !
Today I will teach you to write a simple game :《 Eat everything 》 Let's start at once
Complete material for all articles + The source code is at the end of the article hao Take it yourself !

Text
This paper is based on pygame Write a simple interface of the small game ha !
One 、 Running environment
1) Environmental installation Python3、 Pycharm 、tkinter、Pygame The module is not displayed if it comes with its own module .
( To install the package 、 Activation code, etc I can install it by private mail, and I can answer all the questions ~)
Third party library installation :pip install pygame perhaps With mirror source pip install -i
https://pypi.douban.com/simple/ + Module name 2) material ( picture : Food and people who eat )

I don't think the food is very suitable for beautiful girls , Ha ha ha , Make do with it , You can change the picture !
Two 、 The main program
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(" Song style ",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()summary
hi , The effect is very simple , I won't show you just a picture , The left mouse button has been moving to eat the dropped food !
Free of charge Source base ——
Private letter editor 06 Click on the blue font for free !
Your support is my biggest motivation !! Remember Sanlian ~mua Welcome to read previous articles ~
Some previous articles recommend ——
project 1.5 Pygame Little games : The plant vs. zombie game really has “ poison ”? I can't quit ~
project 1.7 【Pygame Little games 】 Divine reduction 【 Happy double tank battle 】 Small program game , Start playing ~
A summary of the article ——
project 1.0 Python—2021 | Summary of existing articles | Continuous updating , Just read this article directly
( More + The source code is summarized in the article !! Welcome to ~)
A summary of the article ——
( There are more cases waiting for you to learn ~ You can find me for free !)

边栏推荐
- Problems encountered in vs compilation
- 2022-2-15 learning the imitated Niuke project - Section 5 shows comments
- What is the open source compatibility of the current version of polardb-x? mysql8?
- On traversal of tree nodes
- 守护雪山之王:这些AI研究者找到了技术的新「用武之地」
- February 16, 2022 freetsdb compilation and operation
- Pisa-Proxy 之 SQL 解析实践
- Create a database and use
- 机械硬盘和ssd固态硬盘的原理对比分析
- 洛谷_P1003 [NOIP2011 提高组] 铺地毯_暴力枚举
猜你喜欢

Problems encountered in vs compilation

Leetcode daily practice (Yanghui triangle)

Eolink launched a support program for small and medium-sized enterprises and start-ups to empower enterprises!

保留有效位数;保留小数点后n位;

Open source 23 things shardingsphere and database mesh have to say

What is the London Silver unit
![洛谷_P1003 [NOIP2011 提高组] 铺地毯_暴力枚举](/img/65/413ac967cc8fc22f170c8c7ddaa106.png)
洛谷_P1003 [NOIP2011 提高组] 铺地毯_暴力枚举

E modulenotfounderror: no module named 'psychopg2' (resolved)

Vulnerability recurrence ----- 34. Yapi remote command execution vulnerability

3.2 multiple condition judgment
随机推荐
Design of electronic calculator system based on FPGA (with code)
Vulnerability recurrence ----- 34. Yapi remote command execution vulnerability
E ModuleNotFoundError: No module named ‘psycopg2‘(已解决)
Leetcode daily practice (Yanghui triangle)
[issue 17] golang's one-year experience in developing Meitu
February 16, 2022 freetsdb compilation and operation
一场分销裂变活动,不止是发发朋友圈这么简单!
FPGA based analog I ² C protocol system design (with main code)
Scrapy framework (I): basic use
可变参数模板 Variadic Templates
Sigkdd22 | graph generalization framework of graph neural network under the paradigm of "pre training, prompting and fine tuning"
E modulenotfounderror: no module named 'psychopg2' (resolved)
Create a database and use
关于快速幂
sql注入原理
Design of digital video signal processor based on FPGA (with main code)
SIGKDD22|图“预训练、提示、微调”范式下的图神经网络泛化框架
Different perspectives
SQL injection principle
Design of CAN bus controller based on FPGA (with main codes)