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

边栏推荐
- 说明 | 华为云云商店「商品推荐榜」
- 《QT+PCL第六章》点云配准icp系列6
- JS中箭头函数和普通函数的区别
- [video memory optimization] deep learning video memory optimization method
- 6.2 normalization 6.2.6 BC normal form (BCNF) 6.2.9 normalization summary
- 《QT+PCL第九章》点云重建系列2
- 【STM32-USB-MSC问题求助】STM32F411CEU6 (WeAct)+w25q64+USB-MSC Flash用SPI2 读出容量只有520KB
- ThinkPHP advanced
- 华为发布HCSP-Solution-5G Security人才认证,助力5G安全人才生态建设
- Survey of intrusion detection systems:techniques, datasets and challenges
猜你喜欢

MySQL advanced 4

Wechat applet 03 - text is displayed from left to right, and the block elements in the line are centered

Pnas: brain and behavior changes of social anxiety patients with empathic embarrassment

Stm32f4-tft-spi timing logic analyzer commissioning record
![[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)

RT-Thread Env 工具介绍(学习笔记)

异常检测中的浅层模型与深度学习模型综述(A Unifying Review of Deep and Shallow Anomaly Detection)
Sort out the four commonly used sorting functions in SQL

做空蔚来的灰熊,以“碰瓷”中概股为生?

如何实现时钟信号分频?
随机推荐
摩根大通期货开户安全吗?摩根大通期货公司开户方法是什么?
【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测
【一天学awk】条件与循环
你TM到底几点下班?!!!
The last picture is seamlessly connected with the first picture in the swiper rotation picture
Tableapi & SQL and MySQL insert data of Flink
【目标跟踪】|模板更新 时间上下文信息(UpdateNet)《Learning the Model Update for Siamese Trackers》
[Cloudera][ImpalaJDBCDriver](500164)Error initialized or created transport for authentication
What are the test items of juicer ul982
Skywalking 6.4 distributed link tracking usage notes
二叉树的前序,中序,后续(非递归版本)
C#/VB.NET 合并PDF文档
HR面试:最常见的面试问题和技巧性答复
【STM32-USB-MSC问题求助】STM32F411CEU6 (WeAct)+w25q64+USB-MSC Flash用SPI2 读出容量只有520KB
Stm32f4-tft-spi timing logic analyzer commissioning record
Redis秒杀demo
SAP S/4HANA: 一条代码线,许多种选择
将ABAP On-Premises系统连接到中央检查系统以进行自定义代码迁移
swiper 轮播图,最后一张图与第一张图无缝衔接
MySQL审计插件介绍