python使用pygame實現(xiàn)笑臉乒乓球彈珠球游戲-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)公司成都網(wǎng)站建設按需網(wǎng)站策劃,是成都網(wǎng)站建設公司,為成都紗窗提供網(wǎng)站建設服務,有成熟的網(wǎng)站定制合作流程,提供網(wǎng)站定制設計服務:原型圖制作、網(wǎng)站創(chuàng)意設計、前端HTML5制作、后臺程序開發(fā)等。成都網(wǎng)站設計熱線:18982081108

笑臉乒乓球游戲功能介紹

乒乓球游戲功能如下:

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

游戲設計思路

根據(jù)游戲規(guī)則,我們需要

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

代碼實現(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()
#分數(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()

標題名稱:python使用pygame實現(xiàn)笑臉乒乓球彈珠球游戲-創(chuàng)新互聯(lián)
標題URL:http://muchs.cn/article4/egooe.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導航、App開發(fā)、標簽優(yōu)化Google、電子商務、品牌網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設