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

[learn Python by lifting your hands] Xiaobai started a small project with source code

編輯:Python

It's kind of basic , But comrades who don't know what they can do can have a look . Get the material to see the last .

preparation

The following modules are involved

import pygame
import sys
import random
from pygame.locals import *
import time

material : Audio 、 picture 、 typeface

Start writing code

First , Import the module mentioned above , secondly , Create the window and prepare it

pygame.init()
window = pygame.display.set_mode([600, 400])
sur = pygame.Surface([600, 400])
clr = (0, 0, 255)
posAll = [[100, 150], [300, 150], [500, 150], [200, 300], [400, 300]]
rad = 50
tick = 0
pos = posAll[0] 

then , Record score

score = 0
pygame.font.init()
score_font = pygame.font.Font("MicrosoftYaqiHeiLight-2.ttf", 30)
score_sur = score_font.render(str(score), False, (255, 0, 0)) 

Mouse events and ground mice

pygame.mouse.set_visible(False)
mpos = [300, 200]
times = 0
times_max = 10
tick_max = 30
map = pygame.image.load("dds-map.jpg")
rat1 = pygame.image.load("rat1.png")
rat2 = pygame.image.load("rat2.png")
ham1 = pygame.image.load("hammer1.png")
ham2 = pygame.image.load("hammer2.png")
gameover = 0
gameover_max = 100 #

Load music

pygame.mixer.music.load("bg.mp3")
pygame.mixer.music.play(-1)
hitsound = pygame.mixer.Sound("hit.wav")
hurtsound = pygame.mixer.Sound("aiyo2.wav") 

Set the final details

while 1:
hamsur = ham1
ratsur = rat1
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
hamsur = ham2
hitsound.play()
mpos = pygame.mouse.get_pos()
dis = pygame.math.Vector2(mpos[0] - pos[0], mpos[1] - pos[1])
len = pygame.math.Vector2.length(dis)
if len < rad:
tick = 1000
score = score + 1
ratsur = rat2
hurtsound.play()
elif event.type == MOUSEMOTION:
mpos = pygame.mouse.get_pos()
if times >= times_max:
sur.fill((0, 0, 0))
pygame.mouse.set_visible(True)
end_font = pygame.font.Font("MicrosoftYaqiHeiLight-2.ttf", 48)
end_sur = score_font.render(
" Your score is :{}/{}!".format(score, times_max), True, (255, 0, 0)
)
sur.blit(end_sur, (100, 150))
cd = int((gameover_max - gameover) / 10)
cd_sur = score_font.render(
" Start the countdown again {}".format(cd), True, (255, 0, 0)
)
sur.blit(cd_sur, (100, 200))
gameover = gameover + 1
else:
sur.blit(map, (0, 0))
score_sur = score_font.render(
" fraction :{}/{}!".format(score, times + 1), False, (255, 0, 0)
)
sur.blit(score_sur, (200, 10))
if tick > tick_max:
times = times + 1
a = random.randint(0, 4)
pos = posAll[a]
tick = 0
else:
tick = tick + 1
if tick > 5:
sur.blit(ratsur, (pos[0] - 50, pos[1] - 70))
sur.blit(hamsur, (mpos[0] - 50, mpos[1] - 100))
window.blit(sur, (0, 0))
pygame.display.flip()
time.sleep(0.04)
if gameover > gameover_max:
pygame.mouse.set_visible(False)
times = 0
score = 0
gameover = 0

Effect display and material acquisition

The effect is as follows


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