鱼C论坛

 找回密码
 立即注册
查看: 1235|回复: 7

[技术交流] 塔利班Pygame-2-打砖块

[复制链接]
发表于 2018-9-6 14:20:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 塔利班 于 2018-9-6 14:24 编辑

这个游戏实在是太好编了,
让我想起小时候的FC,
没有对速度进行越来越快的
设置,不然感觉是TAS的范畴,
小方块用了精灵,其实也可以画矩形,
但是精灵好检测碰撞
附件大小有限制,发不了整个文件了

#element.py
  1. import pygame
  2. from random import *

  3. #定义挡板类
  4. class Me(pygame.sprite.Sprite):
  5.     def __init__(self,bg_size):
  6.         pygame.sprite.Sprite.__init__(self)

  7.         self.image=pygame.image.load('images/me.png').convert_alpha()
  8.         self.rect=self.image.get_rect()

  9.         self.width,self.height=bg_size
  10.         self.speed=0
  11.         self.mask=pygame.mask.from_surface(self.image)
  12.         self.life=3
  13.         self.score=0
  14.         self.lmove=False
  15.         self.rmove=False
  16.         self.alive=True
  17.         self.catch=True

  18.     def reset(self):
  19.         self.speed=[0,0]
  20.         self.lmove=self.rmove=False
  21.         self.alive=True
  22.         self.rect.left,self.rect.top=130,400
  23.         self.catch=True

  24. #定义球类        
  25. class Ball(pygame.sprite.Sprite):
  26.     def __init__(self,bg_size):
  27.         pygame.sprite.Sprite.__init__(self)
  28.         
  29.         self.image=pygame.image.load('images/ball.png').convert_alpha()
  30.         self.rect=self.image.get_rect()

  31.         self.width,self.height=bg_size
  32.         self.speed=[0,0]
  33.         self.mask=pygame.mask.from_surface(self.image)

  34.     def reset(self):
  35.         self.speed=[0,0]
  36.         self.rect.left,self.rect.bottom=155,400

  37. #定义砖块类
  38. class Brick(pygame.sprite.Sprite):
  39.     def __init__(self,bg_size):
  40.         pygame.sprite.Sprite.__init__(self)
  41.         self.i=randint(1,6)
  42.         self.image=pygame.image.load('images/'+str(self.i)+'.png').convert_alpha()
  43.         self.rect=self.image.get_rect()
  44.         self.width,self.height=bg_size
  45.         self.mask=pygame.mask.from_surface(self.image)

  46.     def set(self,i,j):
  47.         self.rect.left,self.rect.top=i*40,j*10+40
复制代码


#main.py
  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. from random import *
  5. import traceback
  6. from element import *

  7. pygame.init()
  8. pygame.mixer.init()

  9. bg_size=width,height=320,440
  10. screen=pygame.display.set_mode(bg_size)
  11. pygame.display.set_caption('弹球')

  12. bgm=pygame.mixer.Sound('sound/aa.wav')
  13. bgm.set_volume(0.2)
  14. bgm.play(-1)

  15. b_sound=pygame.mixer.Sound('sound/bomb.wav')
  16. b_sound.set_volume(0.2)

  17. w_sound=pygame.mixer.Sound('sound/winner.wav')
  18. w_sound.set_volume(0.2)


  19. WHITE=(255,255,0)

  20. #生成挡板
  21. me=Me(bg_size)
  22. me.reset()

  23. #生成球
  24. ball=Ball(bg_size)
  25. ball.reset()

  26. #生成砖块
  27. bricks=[]
  28. for i in range(8):
  29.     for j in range(10):
  30.         brick=Brick(bg_size)
  31.         brick.set(i,j)
  32.         bricks.append(brick)
  33. clock=pygame.time.Clock()


  34. #定义球撞挡板组
  35. b_g=pygame.sprite.Group()
  36. b_g.add(ball)

  37. #定义字体
  38. zt=pygame.font.Font('font/font.ttf',24)

  39. running=True

  40. end=False

  41. def main():
  42.     end=False
  43.     while running:
  44.         for event in pygame.event.get():
  45.             if event.type==QUIT:
  46.                 pygame.quit()
  47.                 sys.exit()
  48.             if event.type==KEYDOWN:
  49.                 if event.key==K_a:
  50.                     me.lmove=True
  51.                 if event.key==K_d:
  52.                     me.rmove=True
  53.                 if event.key==K_j and me.catch:
  54.                     ball.speed=[3,-3]
  55.                     me.catch=False
  56.             if event.type==KEYUP:
  57.                 if event.key==K_a:
  58.                     me.lmove=False
  59.                 if event.key==K_d:
  60.                     me.rmove=False

  61.         if me.rmove==False and me.lmove==False:
  62.             me.speed=[0,0]
  63.         elif me.lmove==True:
  64.             me.speed=[-5,0]
  65.         elif me.rmove==True:
  66.             me.speed=[5,0]

  67.         me.rect=me.rect.move(me.speed)
  68.         if me.rect.left<=0:
  69.             me.rect.left=0
  70.         if me.rect.right>=me.width:
  71.             me.rect.right=me.width

  72.         ball.rect=ball.rect.move(ball.speed)
  73.         if ball.rect.left<=0:
  74.             ball.speed[0]=abs(ball.speed[0])
  75.         if ball.rect.right>=ball.width:
  76.             ball.speed[0]=-abs(ball.speed[0])
  77.         if ball.rect.top<=0:
  78.             ball.speed[1]=-ball.speed[1]
  79.         if ball.rect.top>440:
  80.             me.life -=1
  81.             me.reset()
  82.             ball.reset()
  83.         #检测球撞板
  84.         b_m_crash=pygame.sprite.spritecollide(me,b_g,False,pygame.sprite.collide_mask)
  85.         if b_m_crash:
  86.             ball.speed[1]=-abs(ball.speed[1])
  87.             distance=(me.rect.right-ball.rect.left)//10
  88.             if distance==0:
  89.                 ball.speed[0]=6
  90.             if distance==1:
  91.                 ball.speed[0]=4
  92.             if distance==2:
  93.                 ball.speed[0]=2
  94.             if distance==3:
  95.                 ball.speed[0]=-2
  96.             if distance==2:
  97.                 ball.speed[0]=-4
  98.             if distance==3:
  99.                 ball.speed[0]=-6

  100.         #检测球撞砖
  101.         for each in bricks:
  102.             b_b_crash=pygame.sprite.spritecollide(each,b_g,\
  103.                             False,pygame.sprite.collide_mask)
  104.             if b_b_crash:
  105.                 bricks.remove(each)
  106.                 b_sound.play()
  107.                 me.score+=1000
  108.                 if ball.rect.left<=each.rect.left or \
  109.                    ball.rect.right>=each.rect.right:
  110.                     ball.speed[0]=-ball.speed[0]
  111.                 if ball.rect.top<=each.rect.top or\
  112.                    ball.rect.bottom>=each.rect.bottom:
  113.                    ball.speed[1]=-ball.speed[1]
  114.         screen.fill((127,127,127))
  115.         for b in bricks:
  116.             screen.blit(b.image,b.rect)
  117.         if not bricks and not end:
  118.             end=True
  119.             me.reset()
  120.             ball.reset()
  121.             bgm.stop()
  122.             w_sound.play()
  123.         screen.blit(me.image,me.rect)

  124.         screen.blit(ball.image,ball.rect)
  125.         score_text=zt.render('Score : %s     Life : %s'%(me.score,me.life),True,WHITE)
  126.         screen.blit(score_text,(10,410))
  127.         pygame.display.flip()
  128.         clock.tick(60)


  129. if __name__=='__main__':
  130.     try:
  131.         main()
  132.     except SystemExit:
  133.         pass
  134.     except:
  135.         traceback.print_exc()
  136.         pygame.quit()
  137.         input()
复制代码

打到弹板反弹x速度由碰撞位置决定
没有设置gameover,
可以拿去改进,因为觉得不难
1.png
2.png
3.png

评分

参与人数 1荣誉 +3 鱼币 +3 贡献 +3 收起 理由
破灬王 + 3 + 3 + 3 鱼C有你更精彩^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-20 16:43:08 | 显示全部楼层
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\弹球.py", line 62, in <module>
    from element import *
ModuleNotFoundError: No module named 'element'
请问这是哪里出现问题了呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-20 16:44:29 | 显示全部楼层
lorengo 发表于 2018-9-20 16:43
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (mo ...

element是一个py文件,上面有,请把它和main.py放在一个目录下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-20 17:30:27 | 显示全部楼层
了解了,谢谢!
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\main.py", line 15, in <module>
    bgm=pygame.mixer.Sound('sound/aa.wav')
pygame.error: Unable to open file 'sound/aa.wav'
然后出现这个问题,请问这些文件能提供下吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-20 17:36:37 | 显示全部楼层
这个帖子附件容量到上限了,你随便下个wav,背景音乐就是你想听的了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-20 17:39:05 | 显示全部楼层
lorengo 发表于 2018-9-20 17:30
了解了,谢谢!
Traceback (most recent call last):
  File "C:%users\Administrator\Desktop\main.py", ...

看楼上,wav自己下个喜欢的就行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-20 17:39:10 From FishC Mobile | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-20 17:47:45 | 显示全部楼层
好的,辛苦了,感谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 15:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表