设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

【Pygame 第12课】 屡败屡战

[复制链接]

0

主题

0

好友

65

积分

注册会员

Rank: 2

11#
发表于 2013-9-10 20:45:48 |只看该作者
        text = font.render("Socre:&d"% score,1,(0,0,0))
中间的1 是什么意思呢?

还有之前的screen = pygame.display.set_mode((450, 700), 0, 32)
后面的0, 32??
回复

使用道具 举报

0

主题

0

好友

65

积分

注册会员

Rank: 2

12#
发表于 2013-9-10 20:51:50 |只看该作者
还有在屏幕里面怎么换行吖。。。用“\n” 只显示一个空格
回复

使用道具 举报

0

主题

0

好友

65

积分

注册会员

Rank: 2

13#
发表于 2013-9-10 21:04:13 |只看该作者
哈哈, Crossssssssin 老师节日快乐有木有~~
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

14#
发表于 2013-9-10 23:21:56 |只看该作者
liqing215 发表于 2013-9-10 20:51
还有在屏幕里面怎么换行吖。。。用“\n” 只显示一个空格

pygame的文字显示不能换行,要换行的话,得再用一个font
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

65

积分

注册会员

Rank: 2

15#
发表于 2013-9-11 20:16:00 |只看该作者
crossin先生 发表于 2013-9-10 23:21
pygame的文字显示不能换行,要换行的话,得再用一个font

嗯,好的。谢谢、
回复

使用道具 举报

0

主题

0

好友

62

积分

注册会员

Rank: 2

16#
发表于 2013-9-24 19:55:54 |只看该作者
liqing215 发表于 2013-9-10 20:45
text = font.render("Socre:&d"% score,1,(0,0,0))
中间的1 是什么意思呢?

0,32应该指的是颜色质量是32位 。这个是我猜的,其实你把后面的0,32去掉还是能编译的。

1表示True吧。
text = font.render(question, True, (0,0,0))
这个还请大神再讲解下

回复

使用道具 举报

0

主题

0

好友

101

积分

注册会员

Rank: 2

17#
发表于 2013-10-24 16:51:01 |只看该作者
楼主威武,,,
  1. # -*- coding: utf-8 -*-

  2. import pygame
  3. from sys import exit
  4. import random

  5. pygame.init()
  6. screen = pygame.display.set_mode((450, 600), 0, 32)
  7. background = pygame.image.load("back.jpg").convert()
  8. plane = pygame.image.load("plane.png").convert_alpha()
  9. pygame.display.set_icon(plane)
  10. pygame.display.set_caption("打飞机")

  11. class Enemy:
  12.         def restart(self):
  13.                 #重置敌机位置和速度
  14.                 self.x = random.randint(50, 400)
  15.                 self.y = random.randint(-200, -50)
  16.                 self.speed = random.random() + 0.1
  17.                
  18.         def __init__(self):
  19.                         #初始化
  20.                 self.restart()
  21.                 self.image = pygame.image.load('enemy.png').convert_alpha()

  22.         def move(self):
  23.                 if self.y < 600:
  24.                         #向下移动
  25.                     self.y += 0.3
  26.                 else:
  27.                         #重置
  28.                     self.restart()
  29. #enemy = Enemy()
  30. enemies = []
  31. for i in range(3):
  32.     enemies.append(Enemy())

  33. class Bullet:
  34.         def __init__(self):
  35.                 #初始化成员变量,x,y,image
  36.                 self.x = 0
  37.                 self.y = -1
  38.                 self.image = pygame.image.load('bullet.png').convert_alpha()
  39.                 self.active = False
  40.         def restart(self):
  41.                 mouseX, mouseY = pygame.mouse.get_pos()
  42.                 self.x = mouseX - self.image.get_width() / 2
  43.                 self.y = mouseY - self.image.get_height() /2
  44.                 self.active = True
  45.         def move(self):
  46.                 #处理子弹的运动
  47.                 if self.y < 0:
  48.                         self.active = False
  49.                 if self.active:
  50.                         self.y -= 3         
  51. #创建子弹的list
  52. bullets = []
  53. #向list中添加5发子弹
  54. for i in range(5):
  55.     bullets.append(Bullet())
  56. #子弹总数
  57. count_b = len(bullets)
  58. #即将激活的子弹序号
  59. index_b = 0
  60. #发射子弹的间隔
  61. interval_b = 0

  62. class Plane:
  63.     def restart(self):
  64.         self.x = 200
  65.         self.y = 600
  66.         
  67.     def __init__(self):
  68.         self.restart()
  69.         self.image = pygame.image.load('plane.png').convert_alpha()

  70.     def move(self):
  71.         x, y = pygame.mouse.get_pos()
  72.         x-= self.image.get_width() / 2
  73.         y-= self.image.get_height() / 2
  74.         self.x = x
  75.         self.y = y

  76. plane = Plane()
  77. bullet = Bullet()
  78. enemy = Enemy()
  79. def checkHit(enemy, bullet):
  80.         if (bullet.x > enemy.x and bullet.x < enemy.x + enemy.image.get_width()) and (bullet.y > enemy.y and bullet.y < enemy.y + enemy.image.get_height()):
  81.             enemy.restart()
  82.             bullet.active = False
  83.             return True
  84.         return False

  85. def checkCrash(enemy, plane):
  86.     if (plane.x + 0.7*plane.image.get_width() > enemy.x) and (plane.x + 0.3*plane.image.get_width() < enemy.x + enemy.image.get_width()) and (plane.y + 0.7*plane.image.get_height() > enemy.y) and (plane.y + 0.3*plane.image.get_height() < enemy.y + enemy.image.get_height()):
  87.         return True
  88.     return False

  89. gameover = False
  90. score = 0
  91. Hscore = []
  92. font = pygame.font.SysFont('楷体', 16)
  93. text1 = font.render(u"点击鼠标左键重新开始", 1, (0, 0, 0))

  94. while True:
  95.         for event in pygame.event.get():
  96.                 if event.type == pygame.QUIT:
  97.                     pygame.quit()
  98.                     exit()
  99.                 #判断在gameover状态下点击了鼠标
  100.                 if gameover and event.type == pygame.MOUSEBUTTONUP:
  101.                     #重置游戏
  102.                     plane.restart()
  103.                     for e in enemies:
  104.                         e.restart()
  105.                     for b in bullets:
  106.                         b.active = False
  107.                     score = 0
  108.                     gameover = False
  109.         screen.blit(background, (0,0))  
  110.         if not gameover:
  111.                 interval_b -= 1
  112.                 if interval_b < 0:
  113.                         bullets[index_b].restart()
  114.                         interval_b = 100
  115.                         index_b = (index_b+1) % count_b
  116.                 for b in bullets:
  117.                         if b.active:
  118.                                 for e in enemies:
  119.                                         if checkHit(e, b):
  120.                                                 score += 100
  121.                                         if checkCrash(e, plane):
  122.                                                 gameover = True
  123.                                        
  124.                                 b.move()
  125.                                 screen.blit(b.image, (b.x-23, b.y))
  126.                                 screen.blit(b.image, (b.x+25, b.y))
  127.                 for e in enemies:
  128.                         e.move()
  129.                         screen.blit(e.image, (e.x, e.y))
  130.                 plane.move()
  131.                 screen.blit(plane.image, (plane.x, plane.y))
  132.                 text = font.render("Socre:%d" % score, 1, (0, 0, 0))
  133.                 screen.blit(text, (0, 0))
  134.         else:
  135.                 screen.blit(text, (0, 0))
  136.                 screen.blit(text1,(100,32))
  137.                 Hscore.append(score)
  138.                 text2 = font.render(u"HScore:%d" % sorted(Hscore)[-1], 1, (0, 0, 0))
  139.                 screen.blit(text2, (250, 0))
  140.                 pass
  141.         pygame.display.update()

复制代码
回复

使用道具 举报

0

主题

0

好友

558

积分

高级会员

Rank: 4

18#
发表于 2014-9-20 23:20:41 |只看该作者
哈哈哈哈,终于成功了。
学习,纯粹。
回复

使用道具 举报

0

主题

0

好友

558

积分

高级会员

Rank: 4

19#
发表于 2014-9-21 00:22:31 |只看该作者
成功了,打呀打呀打呀打呀打呀打呀。
学习,纯粹。
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

20#
发表于 2014-9-21 15:24:14 |只看该作者
liu-pengfei 发表于 2014-9-21 00:22
成功了,打呀打呀打呀打呀打呀打呀。

为你点赞!
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-4-20 02:33 , Processed in 0.028847 second(s), 21 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部