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

边栏推荐
- Using swiper to make mobile phone rotation map
- 药品溯源夯实安全大堤
- 如何写出好代码 - 防御式编程指南
- Wechat applet 03 - text is displayed from left to right, and the block elements in the line are centered
- 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)
- 点云重建方法汇总一(PCL-CGAL)
- "Qt+pcl Chapter 6" point cloud registration ICP Series 6
- Pico,能否拯救消费级VR?
猜你喜欢

Photoshop plug-in HDR (II) - script development PS plug-in
SQL常用的四个排序函数梳理

TensorFlow团队:我们没被抛弃

微信小程序02-轮播图实现与图片点击跳转

Tiantou village, Guankou Town, Xiamen special agricultural products Tiantou Village special agricultural products ant new village 7.1 answer

S32K1xx 微控制器的硬件设计指南

Returning to the top of the list, the ID is still weak
Implementation of deploying redis sentry in k8s

Summary of point cloud reconstruction methods I (pcl-cgal)

Detailed explanation of stm32adc analog / digital conversion
随机推荐
Filter & (login interception)
Implementation of deploying redis sentry in k8s
Flink 系例 之 TableAPI & SQL 与 Kafka 消息插入
Zhang Chi Consulting: household appliance enterprises use Six Sigma projects to reduce customers' unreasonable return cases
Microservice tracking SQL (support Gorm query tracking under isto control)
Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析
她就是那个「别人家的HR」|ONES 人物
Qt+pcl Chapter 6 point cloud registration ICP series 3
SAP s/4hana: one code line, many choices
张驰咨询:家电企业用六西格玛项目减少客户非合理退货案例
JS中箭头函数和普通函数的区别
The difference between arrow function and ordinary function in JS
Tableapi & SQL and Kafka message insertion in Flink
A unifying review of deep and shallow anomaly detection
华为发布HCSP-Solution-5G Security人才认证,助力5G安全人才生态建设
Pico,能否拯救消费级VR?
如何实现时钟信号分频?
Wechat applet 02 - Implementation of rotation map and picture click jump
Shopping mall 6.27 to be completed
Beilianzhuguan joined the dragon lizard community to jointly promote carbon neutralization