当前位置:网站首页>[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 !)
边栏推荐
- 做空蔚来的灰熊,以“碰瓷”中概股为生?
- 张驰咨询:家电企业用六西格玛项目减少客户非合理退货案例
- 使用swiper制作手机端轮播图
- Wechat official account subscription message Wx open subscribe implementation and pit closure guide
- Wechat applet 02 - Implementation of rotation map and picture click jump
- Pnas: brain and behavior changes of social anxiety patients with empathic embarrassment
- SQL常用的四个排序函数梳理
- RT-Thread Env 工具介绍(学习笔记)
- Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan
- Create employee data in SAP s/4hana by importing CSV
猜你喜欢
异常检测中的浅层模型与深度学习模型综述(A Unifying Review of Deep and Shallow Anomaly Detection)
MySQL高级篇4
Survey of intrusion detection systems:techniques, datasets and challenges
点云重建方法汇总一(PCL-CGAL)
Stm32f4-tft-spi timing logic analyzer commissioning record
MySQL backup and restore single database and single table
【Pygame实战】你说神奇不神奇?吃豆人+切水果结合出一款你没玩过的新游戏!(附源码)
phpcms后台上传图片按钮无法点击
Microservice tracking SQL (support Gorm query tracking under isto control)
Intelligent operation and maintenance practice: banking business process and single transaction tracking
随机推荐
[one day learning awk] conditions and cycles
Flink 系例 之 TableAPI & SQL 与 Kafka 消息插入
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(三)
STM32F411 SPI2输出错误,PB15无脉冲调试记录【最后发现PB15与PB14短路】
Qt+pcl Chapter 6 point cloud registration ICP series 3
Deep operator overloading (2)
Day 3 of rhcsa study
使用swiper制作手机端轮播图
Redis seckill demo
《QT+PCL第六章》点云配准icp系列3
Short Wei Lai grizzly, to "touch China" in the concept of stocks for a living?
"Qt+pcl Chapter 6" point cloud registration ICP Series 6
【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
Wechat applet 02 - Implementation of rotation map and picture click jump
SAP S/4HANA: 一条代码线,许多种选择
Lean Six Sigma project counseling: centralized counseling and point-to-point counseling
【目标跟踪】|模板更新 时间上下文信息(UpdateNet)《Learning the Model Update for Siamese Trackers》
Wechat official account subscription message Wx open subscribe implementation and pit closure guide
【显存优化】深度学习显存优化方法