python编程之bomb catcher 小游戏

来源:互联网 发布:macbook如何删除软件 编辑:程序博客网 时间:2024/05/17 13:40

一个简单的演示,综合了鼠标的输入、一些基本的图形绘制等等。当炸弹到达屏幕底端的时候如果没有抓住的话会丢掉性命。如果撞击到挡板,玩家就算抓住了炸弹,另一个炸弹也会落下。


源代码如下:

# -*- coding: utf-8 -*-"""Created on Tue Jun 30 23:42:49 2015@author: liuchang"""import pygame,sys,randomfrom pygame.locals import *pygame.init()color= 255,255,0#绘制圆心的位置positionx=300positiony=0radius = 30width=5vel_x = 1vel_y = 1rec_x=250rec_y=500rec_a=100rec_b=20mouse_x=0mouse_y=0lives = 10score = 0#判断是否需要重启游戏game_over =False font1 =pygame.font.Font(None,30) screen= pygame.display.set_mode((600,520))pygame.display.set_caption("bomb game")def print_text(font,x,y,text,color=(255,255,255)):    imgText=font.render(text,True,color)    screen.blit(imgText,(x,y))while True:          for event in pygame.event.get():         if event.type in (QUIT,KEYDOWN):             sys.exit()         elif event.type== MOUSEMOTION:             mouse_x,mouse_y = event.pos     #print positionx       positionx +=vel_x     positiony+=vel_y              if positionx<radius or positionx>600-radius:             vel_x =- vel_x     if positiony<0 or positiony>500-radius:             vel_y =- vel_y         screen.fill((0,0,200))       #draw boom     pygame.draw.circle(screen,color ,(positionx,positiony),radius,0)     #draw rectangle     rec_x = mouse_x     if rec_x <0:         rec_x=0     elif rec_x > 500:         rec_x = 500     pygame.draw.rect(screen,color,(rec_x,rec_y,rec_a,rec_b),0)          #score and lives      if positiony == 500-radius :         if positionx>rec_x and positionx<rec_x+100:             score+=1             positionx = random.randint(0,500)             positiony=0         else:              lives -=1             positionx = random.randint(0,500)             positiony=0             if lives == 0:                 game_over =True                 print  "game over"     if game_over :      print_text(font1,100,200,"<click to play>")      lives=0      score = 0                  #draw score and lives     print_text(font1,0,0,"lives: "+str(lives/2))     print_text(font1,500,0,"score :"+str(score/2))          pygame.display.update()



截图:




0 0