当前位置:网站首页>[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 !)

边栏推荐
- Summary of week 22-06-26
- MySQL高级篇4
- 将ABAP On-Premises系统连接到中央检查系统以进行自定义代码迁移
- Go zero actual combat demo (I)
- 【OpenCV 例程200篇】216. 绘制多段线和多边形
- STM32F411 SPI2输出错误,PB15无脉冲调试记录【最后发现PB15与PB14短路】
- Introduction to MySQL audit plug-in
- [cloud trend] new wind direction in June! Cloud store hot list announced
- 智能运维实战:银行业务流程及单笔交易追踪
- JS中箭头函数和普通函数的区别
猜你喜欢
随机推荐
她就是那个「别人家的HR」|ONES 人物
[STM32 learning] w25qxx automatic judgment capacity detection based on STM32 USB storage device
ThinkPHP进阶
软件测试的可持续发展,必须要学会敲代码?
Tableapi & SQL and Kafka message acquisition of Flink example
k8s部署redis哨兵的实现
Detailed explanation of stm32adc analog / digital conversion
Tableapi & SQL and MySQL grouping statistics of Flink
Wechat applet 01 bottom navigation bar settings
Tiantou village, Guankou Town, Xiamen special agricultural products Tiantou Village special agricultural products ant new village 7.1 answer
张驰咨询:家电企业用六西格玛项目减少客户非合理退货案例
SQL常用的四个排序函数梳理
《QT+PCL第六章》点云配准icp系列3
使用 csv 导入的方式在 SAP S/4HANA 里创建 employee 数据
说明 | 华为云云商店「商品推荐榜」
Research on manually triggering automatic decision of SAP CRM organization model with ABAP code
微信小程序01-底部导航栏设置
Redis high availability principle
The difference between arrow function and ordinary function in JS
Zhang Chi Consulting: lead lithium battery into six sigma consulting to reduce battery capacity attenuation







