当前位置:网站首页>[pyGame practice] do you think it's magical? Pac Man + cutting fruit combine to create a new game you haven't played! (source code attached)
[pyGame practice] do you think it's magical? Pac Man + cutting fruit combine to create a new game you haven't played! (source code attached)
2022-07-01 15:39:00 【Gu Muzi acridine】
Introduction
Hey ! Mumuzi flashed today —— I have written a lot for you ~
AI involved 、 beginner 、 Reptiles 、 Data analysis ( This aspect is generally just an audit ) game ........
Through the reading volume of so many articles, there is still more reading volume of AI and games !
SO The recent content has just begun to be updated, or is it ready to start with the game , After all, many friends must have been unable to wait ~
Follow up slowly update the content of artificial intelligence ~ To tell you the truth, I don't know what to update , But we will continue to work hard
Learning new knowledge is more civilized !FigthingFigthingFigthing、
Complete material for all articles + The source code is in
Public at the end of the article hao Take it yourself

PS:
I have written about pac man
I have written about cutting fruit
Today the two come together , Make a new game , Ha ha ha , The name is called 《 insane 🤪 Eat fruit 》 Little games , Actually, it sounds very
Of , But the effect is not as tall as expected !( Give you a vaccination )
Text
This paper is based on Pygame Write a game ha !
One 、 In preparation
1) How to play the game
Random drop : Watermelon plus points 、 Grape minus points 、 The initial health of a bomb is two . Move the right mouse button . Add and subtract more
Less specific, just wait for you to play , It's not fun to be spoiled ! Every game code is reserved for you
At the end of , Hee hee , Feel for yourself ~
2) Environmental installation
The environment used by Xiaobian :Python3、Pycharm Community Edition 、tkinter、Pygame modular , Partly from No expansion with modules
in .( It won't be installed : Xiaobian will give you a novice gift bag , Including installation package 、 video 、 answer You can ask me if you have any questions
At the end of the article )
Module installation :pip install -i https://pypi.douban.com/simple/+ Module name 3) Material preparation
The background music is more exciting ! Remember seven This song , It's very pleasant .

Prepared materials, pictures, objects falling from the background, etc .

Two 、 Code display
There are so many codes ! Just show the part , Take it for free at the end of the article !
1) The main program
import tkinter
import random
import time
import Param
import Image
import Bonus
import Deduction
import Bean
import Bomb
import pygame
# Define the list of substances ( Including plus watermelon and minus grapes and bombs )
bonusth = []
deductionth = []
bigbombs = []
# Definition bean Variable , Save Doudou object
bean = ""
# Define the initial score of the current user
score = 0
life = 2
# Define the game state
game_state = Param.GAME_START
# Create a form
game_window = tkinter.Tk()
# Window text settings
game_window.title('I LOVE FRUIT')
# Window position processing
screenwidth = game_window.winfo_screenwidth()
screenheight = game_window.winfo_screenheight()
size = '%dx%d+%d+%d' % (Param.GAME_WIDTH, Param.GAME_HEIGHT, (screenwidth-Param.GAME_WIDTH)/2, 50)
game_window.geometry(size)
# Load all the pictures used in the game
background_image,bean_image,Bonus_image,Bomb_image,Deduction_image= Image.load_image(tkinter)
Start,Stop = Image.load_state_image(tkinter)
# Get canvas
window_canvas = tkinter.Canvas(game_window)
# Canvas packaging
window_canvas.pack(expand=tkinter.YES, fill=tkinter.BOTH)
# Time stamp
count = 0
num = 30
def create_fruit():# Produce fruit
global count
global num
global score
if score % 10 ==1:
if num >= 8:
num -= 8
count += 1
if count % num == 0:
c = random.randint(1,10)
if c <= 5:
# Bonus fruit generation
bonus = Bonus.Bonus(Bonus_image)
bonusth.append(bonus) # Substances added to the list
window_canvas.create_image(bonus.x,bonus.y,anchor = tkinter.NW,image=bonus.image,tag=bonus.tag)
elif c<=8:
# Distribution of fruit production
deduction = Deduction.Deduction(Deduction_image)
deductionth.append(deduction)
window_canvas.create_image(deduction.x,deduction.y,anchor = tkinter.NW,image=deduction.image,tag=deduction.tag)
else:
# Bomb generation
bigbomb = Bomb.BigBomb(Bomb_image)
bigbombs.append(bigbomb)
window_canvas.create_image(bigbomb.x,bigbomb.y,anchor = tkinter.NW,image=bigbomb.image,tag=bigbomb.tag)
def step_fruit():
# Traverse all matter , Call the move method
for bonus in bonusth:
bonus.step(window_canvas)
for deduction in deductionth:
deduction.step(window_canvas)
for bigbomb in bigbombs:
bigbomb.step(window_canvas)
def judge_state(event):
global game_state
if game_state == Param.GAME_START:
game_state = Param.GAME_RUNNING
# Draw points
window_canvas.create_text(20, 20, text=" fraction :%d" % (score), anchor=tkinter.NW, fill="white",\
font="time 12 bold",tag="SCORE")
# Painting life
window_canvas.create_text(20, 50, text=" life :%d" % (life), anchor=tkinter.NW, fill="white",\
font="time 12 bold",tag="LIFE")
# Delete startup picture
window_canvas.delete("Start")
elif game_state == Param.GAME_STOP:
window_canvas.delete("bean")
window_canvas.delete("STOP")
game_state = Param.GAME_START
game_start()
def bean_move(event):
if game_state == Param.GAME_RUNNING:
now_x = bean.x
now_y = bean.y
bean.x = event.x - bean.w/2
bean.y = event.y - bean.h/2
window_canvas.move("bean", bean.x-now_x, bean.y-now_y)
def out_of_bounds():
# Get all the substances , Judge whether you crossed the line
for deduction in deductionth:
if deduction.out_of_bounds():
window_canvas.delete(deduction.tag)
deductionth.remove(deduction)
for bonus in bonusth:
global outnum
if bonus.out_of_bounds():
outnum += 1
window_canvas.delete(bonus.tag)
bonusth.remove(bonus)
if outnum >= 5:
game_state = Param.GAME_STOP
# Draw the end of the game
game_over()
for bigbomb in bigbombs:
if bigbomb.out_of_bounds():
window_canvas.delete(bigbomb.tag)
bigbombs.remove(bigbomb)
def bomb_action():
global score
global life
global bean
global game_state
# Bonus points
for bonus in bonusth:
if bonus.bomb(bean):
window_canvas.delete(bonus.tag)
bonusth.remove(bonus)
score += 3
# points
for deduction in deductionth:
if deduction.bomb(bean):
window_canvas.delete(deduction.tag)
deductionth.remove(deduction)
if score - 5 < 0:
score = 0
game_state = Param.GAME_STOP
# Draw the end of the game
game_over()
else:
score -= 5
for bigbomb in bigbombs:
if bigbomb.bomb(bean):
window_canvas.delete(bigbomb.tag)
bigbombs.remove(bigbomb)
# If the score or life is less than 0 Game over
if life - 1 <= 0:
life = 0
game_state = Param.GAME_STOP
# Draw the end of the game
game_over()
else:
life -= 1
def draw_action():
# Draw points
window_canvas.delete("SCORE")
# Painting life
window_canvas.delete("LIFE")
window_canvas.create_text(20,20,text=" fraction :%d"%(score),anchor=tkinter.NW,fill="white",font="time 12 bold",tag="SCORE")
window_canvas.create_text(20,50,text=" life :%d"%(life),anchor=tkinter.NW,fill="white",font="time 12 bold",tag="LIFE")
def game_over():
global game_state
game_state = Param.GAME_STOP
for deduction in deductionth:
window_canvas.delete(deduction.tag)
for bonus in bonusth:
window_canvas.delete(bonus.tag)
for bigbomb in bigbombs:
window_canvas.delete(bigbomb.tag)
deductionth.clear()
bonusth.clear()
bigbombs.clear()
window_canvas.create_image(0,0,anchor=tkinter.NW,image=Stop,tag="STOP")
if pygame.mixer.music.get_busy() == True:
pygame.mixer.music.stop()# Stop playing
def game_start():
global score
global life
global num
global outnum
num = 30
score = 0
life = 2
outnum = 0
# Draw game background
window_canvas.create_image(0, 0, anchor=tkinter.NW, image=background_image, tag="background")
# Create a Doudou object
global bean
bean = Bean.Bean(bean_image)
window_canvas.create_image(bean.x, bean.y, anchor=tkinter.NW, image=bean.image, tag="bean")
window_canvas.create_image(0, 0, anchor=tkinter.NW, image=Start, tag="Start")
pygame.mixer.init()
pygame.mixer.music.load('Seve( Piano version ).mp3') # Load background music
if pygame.mixer.music.get_busy() == False:
pygame.mixer.music.play(300,0)# repeat 300 Time , Play from the first second
def game():
if game_state == Param.GAME_START:
game_start()
# Mouse monitor
window_canvas.bind("<Motion>",bean_move)
window_canvas.bind("<Button-1>",judge_state)
while True:
if game_state == Param.GAME_RUNNING:
# Material entry
create_fruit()
# Matter moves
step_fruit()
# Delete substances that cross the boundary
out_of_bounds()
# Detect collision
bomb_action()
if score >= 0:
# Painting and life
draw_action()
# Update display
game_window.update()
time.sleep(0.04)
if __name__ == "__main__":
game()
game_window.mainloop()3、 ... and 、 Effect display
1) Game interface

2) Random screenshot

3) Consumption ends

summary
Uh huh ~ This crazy fruit eating game is officially over here , The public at the end of the old rule source code hao I went to get it myself !
It's all free , Welcome to learn and make progress ~
Complete free source code collection office : Find me ! At the end of the article, you can get it by yourself , Didi, I can also !
Some previous articles recommend ——
project 1.3 Video player
project 2.7 Scratch card applet
project 3.2 Automatic wallpaper change
project 3.3 WordArt signature
project 3.9 Code live in the snow all over the sky, snow applet
project 4.0 GIF Make magic ( Take Douluo as an example )
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 !)

边栏推荐
- Tableapi & SQL and MySQL grouping statistics of Flink
- [one day learning awk] conditions and cycles
- k8s部署redis哨兵的实现
- 微信小程序02-轮播图实现与图片点击跳转
- phpcms后台上传图片按钮无法点击
- MySQL 服务正在启动 MySQL 服务无法启动解决途径
- Summary of point cloud reconstruction methods I (pcl-cgal)
- Équipe tensflow: Nous ne sommes pas abandonnés
- GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
- Day 3 of rhcsa study
猜你喜欢

张驰课堂:六西格玛数据的几种类型与区别
Redis high availability principle

Skywalking 6.4 distributed link tracking usage notes

你TM到底几点下班?!!!

STM32ADC模拟/数字转换详解

Zhang Chi's class: several types and differences of Six Sigma data

Introduction to MySQL audit plug-in

C#/VB.NET 合并PDF文档

Reading notes of top performance version 2 (V) -- file system monitoring

GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
随机推荐
新出生的机器狗,打滚1小时后自己掌握走路,吴恩达开山大弟子最新成果
智能运维实战:银行业务流程及单笔交易追踪
《QT+PCL第六章》点云配准icp系列3
Guide de conception matérielle du microcontrôleur s32k1xx
【云动向】6月上云新风向!云商店热榜揭晓
做空蔚来的灰熊,以“碰瓷”中概股为生?
JS中箭头函数和普通函数的区别
[advanced ROS] lesson 5 TF coordinate transformation in ROS
点云重建方法汇总一(PCL-CGAL)
Qt+pcl Chapter 6 point cloud registration ICP series 3
Qt+pcl Chapter 6 point cloud registration ICP series 4
Day 3 of rhcsa study
《QT+PCL第六章》点云配准icp系列4
跨平台应用开发进阶(二十四) :uni-app实现文件下载并保存
Phpcms background upload picture button cannot be clicked
七夕表白攻略:教你用自己的专业说情话,成功率100%,我只能帮你们到这里了啊~(程序员系列)
[antenna] [3] some shortcut keys of CST
二叉树的前序,中序,后续(非递归版本)
Flink 系例 之 TableAPI & SQL 与 Kafka 消息插入
Tableapi & SQL and MySQL data query of Flink