程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Eating games implemented in Python

編輯:Python

一、設計目的

課程設計教學目的

本課程設計是本專業的一門重要實踐性教學環節.在學習了專業基礎課和《Python程序設計》課程的基礎上,本課程設計旨在加深對Python程序設計的認識,對Python語言及其語言生態有一個進一步的掌握和應用,學會運用Python標准庫及外接相關庫來解決實際問題的基本能力,培養和提高學生分析問題、解決問題的能力,尤其是提高學生使用Python為開發語言來進行問題描述、交流與思考的能力,為畢業設計和以後的工程實踐打下良好的基礎.

本課程設計具體目的

  1. for a comprehensive understandingPython技術歷史、On the basis of the status quo and development trend,系統掌握Python基本概念、Programming ideas and programming techniques,skilledPythonProgramming skills and technical ideas of object-oriented software design.

  2. From overall framework to page design to code implementation,了解Python設計的一般步驟,並熟練掌握Python的設計思想.

  3. This subject mainly trains logical thinking andPython語法,Write code from big to small,From general to module,master its basic logic,It can be used to draw inferences about future designs,to form logical thinking.

二、設計任務和要求

課程設計教學任務和要求

The main task of this course design is toPython為開發語言完成一個100~300行左右規模的程序項目開發.See appendix for design reference topics1.

課程設計的基本要求是:在課程設計的各個階段嚴格、規范地完成相關的文檔,e.g. overall programme report,詳細設計報告、功能說明、數據結構說明、算法說明、程序設計框圖、Legends and source programs, etc..It is required that the structure of the written document is reasonable、內容完整、敘述清晰.The source code of the program should have detailed comments,可讀性好.更高要求是:有創意、系統界面美觀.

本課程設計具體任務和要求:

  1. page perfect,邏輯清晰.
  2. The code is written as concisely as possible with high readability.
  3. 可選用PyCharm,Python IDLEand other development platforms to improve development efficiency,Master one or more integrated development environments as much as possible through data review and learning.
  4. Co-finish the connection of the code,Implementation of the overall framework and logic of the game.Division of labor to write curriculum design reports,The report includes a total of six parts:設計目的、設計要求、總體設計、詳細設計、調試與測試、設計總結.

三、總體設計

I am responsible for the implementation of the game logic.

Vel_yis the speed at which the fish falls in the game,The game every time received a bomb,The expression of a cat will be crying face,At the same time, the life value is reduced by one,When missing fish,Life will be reduced by one,當生命值為0時,游戲結束.

四、設計實現

4.1 最終實現結果:

The start of the game page:

game interface:

當Round>2時,will insert bombs:

圖3 bomb interface

4.2 實現結果評價

良好

總體上來說,The operation of the game basically meets the original design concept,meet design requirements.

五、詳細設計

Implementation of the specific logic of the game:

5.1 pygame事件

pygameEvents can handle various things in the game,pygameThe event processing in is done in a real-time loop.put the code in onewhile True的循環中,But this will create an infinite loop,So add a sentencesys.exit()來退出.

for event in pygame.event.get()

The above code will create a list of currently pending events,然後使用forloop to iterate over the events inside.這樣,We will perform different operations in sequence according to the order in which the events are generated.A common event is a key press,Key release and mouse movement.Usually needs to be processed firstQUIT事件(This event is generated when the user closes the window.)

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

5.2 鼠標事件

pygameSupport some mouse events,他們包括:

MOUSEMOTION,MOUSEBUTTONUP,MOUSEBUTTONDOWN.

在MOUSEMOTIONcontains some properties:event.pos,event.rel,event.buttons

Elif event.type == MOUSEMOTION:
mouse_x,mouse_y = event.pos
move_x,move_y = event.rel

MOUSEBUTTONUP裡面的屬性:

elif event.type == MOUSEBUTTONUP:
if game_over:
game_over = False
lives = 10
score = 0
Round =1
vel_y=0.4
mine=0
flag=0
pic=cat
bomb_y = -50

5.3 輪詢鍵盤

在pygame中,使用pygame.key.get_pressed()來輪詢鍵盤接口.This method returns a list of boolean values,where one flag per key.Use key constant values ​​to match key presses,This has the advantage of being able to detect multiple key presses without having to traverse the event system.

keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()

5.4 Game over handling

if game_over:
screen.blit(init,(60, 60))
print_text(font3, 200, 400,"Clicked To Play!")
print_text(font2, 310, 480,"[email protected] developed by xiaoxiami")

圖4 游戲結束頁面

5.5 Game level settings and difficulty settings

等級123456分數<300300600600900900120012001500>1500下落速度0.40.60.81.01.21.2
else:
#Round setting
if score >300 and score <600:
Round=2
elif score >600 and score <900:
Round =3
elif score >900 and score <1200:
Round=4
elif score >1200 and score <1500:
Round =5
elif score >=1500:
Round =6
#draw the Round
print_text(font1, 280, 0, "Round: " + str(Round))
#speed setting
if Round ==1:
vel_y=0.4
elif Round ==2:
vel_y=0.6
elif Round ==3:
vel_y=0.8
elif Round ==4:
vel_y=1.0
elif Round ==5:
vel_y=1.2

5.6 Introduction to the game

If you miss the fish,just reset the fish's position,give it a randomx值,Then the life value is reduced by one:

 if bomb_y > 500:
bomb_x = random.randint(0, 500)
bomb_y = -50
lives -= 1
if lives == 0:
game_over = True

Simple collision detection function,Check to see if the fish is caught:

 elif bomb_y > pos_y:
if bomb_x > pos_x-10 and bomb_x < pos_x + 70:
score += 10
bomb_x = random.randint(0, 500)
bomb_y = -50

The detection of the same bomb is similar to this:

if mine_y > 500:
mine_x = random.randint(0, 500)
mine_y = -50

In order to control the coordinates of the cat not to exceed the screen range,加入了以下代碼:

pos_x = mouse_x
if pos_x < 0:
pos_x = 0
elif pos_x > 510:
pos_x = 500

cat getting bomb.or health less than5的時候,will become crying,So we also need to load a bitmap of the crying face,Then add some corresponding logic code to the program:

Load the cat's crying face bitmap:

cat2=pygame.image.load("aodamiao_3.png")

when the bomb,cat becomes crying face:

elif mine_y > pos_y:
if mine_x > pos_x and mine_x < pos_x + 40:
mine_x = random.randint(0, 500)
mine_y = -50
lives-=1
pic=cat2
if lives == 0:
game_over = True

When the cat's life value is less than5時,cat becomes crying face:

if lives<5:
pic=cat2

六、調試與測試

6.1 異常處理

try...except...finally...

Used to determine whether the cat's position is beyond the game window:

try:
print("開始執行try...")
mouse_x>0
mouse_x<510
except ZeroDivisionError as e:
print(“cat out of range”)
finally:
print("執行finally...")
print("END...")

6.2 調試

Print

通過print Output variables that may be wrong,through error messages andprint to modify the outputbug,printAlso need to delete after the test is done

pdb

啟動Python的單步調試器pdb,The program is executed line by line.可以采用pdb.set_trace()在代碼中設置斷點


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved