精心配音,有封面及结尾的一个完整的拦球小游戏。英文名一般为ping pong game。
以下是部分代码预览:
"""鼠标控制的拦球小游戏,有封面有配音版本,这是用pygame制作的一个小游戏,图中的小球和拦板都是直接画在screen上的.""" import pygame from pygame.locals import * from random import randint class board(): def __init__(self ): self.x = 200 self.y = 300 self.w = 120 self.h = 20 def move(self): mpos = pygame.mouse.get_pos() self.x = mpos[0] - self.w/2 def draw(self): pygame.draw.rect(screen,(0,255,255),(self.x,self.y,self.w,self.h)) class Ball(): def __init__(self,board): self.isalive=True # 描述弹球状态的逻辑变量 self.board = board # 直接在屏幕上画,返回矩形对象 self.shape = pygame.draw.circle(screen,(255,255,0),(screen_width//2,screen_height//2),10,10) self.speed=[randint(-10,10),randint(-10,10)] def collideboard(self): """碰到拦板检测""" pass # 此处是小球碰撞到拦板的代码,请自行编写,亦可联系作者索要 def lostcheck(self): """小球丢失检测""" pass # 请自行编写,亦可向作者索要 def move(self): self.speed[0]=self.speed[0]*1.001 # 这样做是为了让小球移动得越来越快 self.speed[1]=self.speed[1]*1.001 self.shape.move_ip(self.speed[0],self.speed[1]) pass self.lostcheck() # 每次移动后对坐标进行检测,赶过屏幕最底y坐标则标为"死亡" def draw(self): pygame.draw.circle(screen,(255,255,0),(self.shape.centerx,self.shape.centery),10,10) pygame.init() screen_width=480 screen_height=360 screen = pygame.display.set_mode((screen_width,screen_height)) pygame.display.set_caption("pygame制作的拦弹球小游戏_作者:李兴球 ") pygame.mixer.init() 碰撞声 = pygame.mixer.Sound("碰撞.wav") click = pygame.mixer.Sound("click.wav") delete = pygame.mixer.Sound("delete.wav") 碰拦板 = pygame.mixer.Sound("碰好.wav") 失败声 = pygame.mixer.Sound("失败.wav") bgmusic = pygame.mixer.music.load("背景音乐.wav") pygame.mixer.music.play(-1,0) 拦板 = board() 篮子 = [ Ball(拦板),Ball(拦板)] 封面 = pygame.image.load("封面.png") 没单击=True while 没单击: for event in pygame.event.get(): if event.type==MOUSEBUTTONDOWN: click.play() 没单击=False screen.blit(封面,(0,0)) pygame.display.update() font = pygame.font.Font("C:/windows/fonts/msyh.ttf",28) # 微软雅黑字体对象 gameover= font.render("拦球小游戏结束了, 作者:李兴球",True,(255,0,0)) (fx,fy,fw,fh) = gameover.get_rect() clock = pygame.time.Clock() running = True # 下面是游戏循环 while running and not 篮子==[]: # 事件检测 for event in pygame.event.get(): if event.type==QUIT:running = False # 游戏逻辑 拦板.move() [ball.move() for ball in 篮子] # 图形渲染 screen.fill((0,0,0)) 拦板.draw() [ball.draw() for ball in 篮子] # 屏幕更新显示 pygame.display.update() # 检测篮子中有没有"死亡"的小球 for ball in 篮子: if ball.isalive==False: 篮子.remove(ball) clock.tick(30) if 篮子==[]: # 如果小球都丢失了,游戏失败 screen.blit(gameover,(screen_width//2 - fw//2,screen_height//2 - fh//2)) pygame.display.update() 失败声.play() else: pygame.quit()
如需要下载完整源代码及素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)
发表评论