python使用pygame實(shí)現(xiàn)笑臉乒乓球彈珠球游戲
今天我們用python和pygame實(shí)現(xiàn)一個(gè)乒乓球的小游戲,或者叫彈珠球游戲。
笑臉乒乓球游戲功能介紹
乒乓球游戲功能如下:
乒乓球從屏幕上方落下,用鼠標(biāo)來移動(dòng)球拍,使其反彈回去,并獲得得分,如果沒有接到該球,則失去一條命。玩家有一定數(shù)量的命如5。
游戲設(shè)計(jì)思路
根據(jù)游戲規(guī)則,我們需要
1、初始化游戲環(huán)境
2、畫出乒乓球,球拍等
3、設(shè)置乒乓球的運(yùn)動(dòng),并監(jiān)聽鼠標(biāo),以移動(dòng)球拍
4、判斷乒乓球被接住與否
5、游戲是否結(jié)束,是否再玩。
代碼實(shí)現(xiàn)
import pygame
pygame.init()
screen_width=800
screen_height=600
screen=pygame.display.set_mode([screen_width,screen_height])
pygame.display.set_caption("笑臉乒乓球")
keepGoing=True
pic=pygame.image.load("CrazySmile.bmp")
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
picx=0
picy=0
BLACK=(0,0,0)
WHITE=(255,255,255)
timer=pygame.time.Clock()
paddle_width=200
paddle_height=25
paddle_x=300
paddle_y=550
speedx=5
speedy=5
#圖片的高度和寬度
pic_width=pic.get_width()
pic_height=pic.get_height()
#分?jǐn)?shù)和命
points=0
lives=5
font=pygame.font.SysFont("Times",24)
pop = pygame.mixer.Sound("pop.wav")
while keepGoing:
for event in pygame.event.get():
if event.type==pygame.QUIT:
keepGoing=False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_F1: # F1 = New Game
points = 0
lives = 5
picx = 0
picy = 0
speedx = 5
speedy = 5
pop.play()
picx += speedx
picy += speedy
if picx <= 0 or picx >= 700:
speedx = -speedx * 1.1
if picy <= 0:
speedy = -speedy + 1
if picy >= 500:
lives -= 1
speedy = -5
speedx = 5
picy = 499
# if picx <= 0 or picx + pic_width > screen_width:
# speedx = -speedx
# if picy <= 0:
# speedy = -speedy
# if picy >= 500:
# lives -= 1
# speedy = -speedy
screen.fill(BLACK)
screen.blit(pic, (picx, picy))
# 畫出球拍
paddle_x = pygame.mouse.get_pos()[0]
paddle_x -= paddle_width / 2
pygame.draw.rect(screen, WHITE, (paddle_x, paddle_y, paddle_width, paddle_height))
#判斷接住乒乓球
if picy + pic_width > paddle_y and picy + pic_height < paddle_y + paddle_height and speedy > 0:
if picx + pic_width / 2 > paddle_x and picx + pic_width / 2 < paddle_x + paddle_width:
points += 1
speedy = -speedy
# 在屏幕上畫出得分
draw_string = "Lives: " + str(lives) + " Points: " + str(points)
if lives<1:
draw_string="Game Over. Your scores is "+str(points)
draw_string+="press F1 to play again"
text = font.render(draw_string, True, WHITE)
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text_rect.y = 10
screen.blit(text, text_rect)
pygame.display.update()
timer.tick(60)
pygame.quit()
代碼中用的乒乓球是如下圖片。

總結(jié)
1、通過上述代碼,功能基本實(shí)現(xiàn)
2、可以有很多改進(jìn),如通過鍵盤來操控球拍,如給游戲加上背景音樂,其中加音樂的方法是
pop = pygame.mixer.Sound("pop.wav")
pop.play()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python 調(diào)用HBase的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄猵ython 調(diào)用HBase的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12
Windows下的Python 3.6.1的下載與安裝圖文詳解(適合32位和64位)
這篇文章主要介紹了Windows下的Python 3.6.1的下載與安裝圖文詳解(適合32位和64位),需要的朋友可以參考下2018-02-02
Python數(shù)據(jù)可視化之用Matplotlib繪制常用圖形
Matplotlib能夠繪制折線圖、散點(diǎn)圖、柱狀圖、直方圖、餅圖. 我們需要知道不同的統(tǒng)計(jì)圖的意義,以此來決定選擇哪種統(tǒng)計(jì)圖來呈現(xiàn)我們的數(shù)據(jù),今天就帶大家詳細(xì)了解如何繪制這些常用圖形,需要的朋友可以參考下2021-06-06
解決安裝torch后,torch.cuda.is_available()結(jié)果為false的問題
這篇文章主要介紹了解決安裝torch后,torch.cuda.is_available()結(jié)果為false的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
使用python批量修改文件名的方法(視頻合并時(shí))
這篇文章主要介紹了視頻合并時(shí)使用python批量修改文件名的方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
Python THREADING模塊中的JOIN()方法深入理解
這篇文章主要介紹了Python THREADING模塊中的JOIN()方法深入理解,本文用簡(jiǎn)潔易懂的語言總結(jié)了對(duì)JOIN()方法的理解,不同于其它文章,需要的朋友可以參考下2015-02-02
PyQt實(shí)現(xiàn)異步數(shù)據(jù)庫(kù)請(qǐng)求的實(shí)戰(zhàn)記錄
開發(fā)軟件的時(shí)候不可避免要和數(shù)據(jù)庫(kù)發(fā)生交互,但是有些 SQL 請(qǐng)求非常耗時(shí),如果在主線程中發(fā)送請(qǐng)求,可能會(huì)造成界面卡頓,本文將介紹一種讓數(shù)據(jù)庫(kù)請(qǐng)求變得和前端的 ajax 請(qǐng)求一樣簡(jiǎn)單,希望對(duì)大家有所幫助2023-12-12
python的pytest框架之命令行參數(shù)詳解(下)
這篇文章主要介紹了python的pytest框架之命令行參數(shù)詳解,今天將繼續(xù)更新其他一些命令選項(xiàng)的使用,和pytest收集測(cè)試用例的規(guī)則,需要的朋友可以參考下2019-06-06
nlp計(jì)數(shù)法應(yīng)用于PTB數(shù)據(jù)集示例詳解
這篇文章主要為大家介紹了nlp計(jì)數(shù)法應(yīng)用于PTB數(shù)據(jù)集示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04

