麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > Python > 正文

python使用pygame實現笑臉乒乓球彈珠球游戲

2024-09-09 19:02:54
字體:
來源:轉載
供稿:網友

今天我們用python和pygame實現一個乒乓球的小游戲,或者叫彈珠球游戲。

笑臉乒乓球游戲功能介紹

乒乓球游戲功能如下:

乒乓球從屏幕上方落下,用鼠標來移動球拍,使其反彈回去,并獲得得分,如果沒有接到該球,則失去一條命。玩家有一定數量的命如5。

游戲設計思路

根據游戲規則,我們需要

1、初始化游戲環境
2、畫出乒乓球,球拍等
3、設置乒乓球的運動,并監聽鼠標,以移動球拍
4、判斷乒乓球被接住與否
5、游戲是否結束,是否再玩。

代碼實現

import pygamepygame.init()screen_width=800screen_height=600screen=pygame.display.set_mode([screen_width,screen_height])pygame.display.set_caption("笑臉乒乓球")keepGoing=Truepic=pygame.image.load("CrazySmile.bmp")colorkey = pic.get_at((0,0))pic.set_colorkey(colorkey)picx=0picy=0BLACK=(0,0,0)WHITE=(255,255,255)timer=pygame.time.Clock()paddle_width=200paddle_height=25paddle_x=300paddle_y=550speedx=5speedy=5#圖片的高度和寬度pic_width=pic.get_width()pic_height=pic.get_height()#分數和命points=0lives=5font=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()

代碼中用的乒乓球是如下圖片。

總結

1、通過上述代碼,功能基本實現
2、可以有很多改進,如通過鍵盤來操控球拍,如給游戲加上背景音樂,其中加音樂的方法是

pop = pygame.mixer.Sound("pop.wav")pop.play()

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網之家。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久99国产综合精品 | 精选久久 | 国产91中文字幕 | 国产乱弄 | 成人免费一区二区三区在线观看 | 欧美日韩亚洲另类 | 在线看一级片 | 久久久久久中文字幕 | 欧美日韩大片在线观看 | 热久久91 | 欧美一级一区二区三区 | 性爱免费视频 | www.com超碰 | 久久噜噜噜 | 欧美性成人 | 精品国产亚洲人成在线 | 午夜精品成人一区二区 | 亚洲视频在线一区二区 | 中文字幕极速在线观看 | 久草在线小说 | 黄色片网站免费看 | 欧美三级短视频 | 欧美a级在线免费观看 | 91网站在线观看视频 | 麻豆小视频在线观看 | 性欧美大战久久久久久久免费观看 | 国产成人高潮免费观看精品 | 国产精品视频一区二区三区四 | 亚洲片在线 | 中文有码一区二区 | 日韩黄站 | 在线观看视频亚洲 | 四季久久免费一区二区三区四区 | 激情网站在线观看 | 午夜a狂野欧美一区二区 | 国产成人精品区一区二区不卡 | 亚洲一区二区免费 | 中文字幕网站在线 | 国产 视频 一区二区 | 国产精品成人一区二区三区电影毛片 | 精品国产91久久久久久 |