请选择 进入手机版 | 继续访问电脑版
设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生

【Pygame 第6课】 面向对象的游戏设计

[复制链接]

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2013-9-11 11:59:48 |显示全部楼层
test777 发表于 2013-9-11 00:55
感觉可以把self.image.get_width()放到__init__里面去执行, 就不用每次move都计算一次了 ...

对的
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

101

积分

注册会员

Rank: 2

发表于 2013-10-20 00:14:29 |显示全部楼层
两弹齐发。。
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. from sys import exit

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

  10. class Bullet:
  11.     def __init__(self):
  12.         #初始化成员变量,x,y,image
  13.         self.x = 0
  14.         self.y = -1
  15.         self.image = pygame.image.load('bullet.png').convert_alpha()

  16.     def move(self):
  17.         #处理子弹的运动
  18.         if self.y < 0:
  19.             mouseX, mouseY = pygame.mouse.get_pos()
  20.             self.x = mouseX - self.image.get_width() / 2
  21.             self.y = mouseY - self.image.get_height() / 2
  22.         else:
  23.             self.y -= 5
  24. bullet = Bullet()

  25. while True:
  26.     for event in pygame.event.get():
  27.         if event.type == pygame.QUIT:
  28.             pygame.quit()
  29.             exit()
  30.     bullet.move()
  31.     x, y = pygame.mouse.get_pos()
  32.     x -= plane.get_width() / 2
  33.     y -= plane.get_height() / 2
  34.     screen.blit(background, (0,0))
  35.     screen.blit(bullet.image, (bullet.x-23, bullet.y))
  36.     screen.blit(bullet.image, (bullet.x+25, bullet.y))
  37.     screen.blit(plane, (x,y))
  38.     pygame.display.update()
复制代码
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2013-10-20 21:47:48 |显示全部楼层
shallecker 发表于 2013-10-20 00:14
两弹齐发。。

不错的
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

10

积分

新手上路

Rank: 1

发表于 2015-10-18 20:39:02 |显示全部楼层
请问一个问题,我习惯写c,刚开始写python,用的是自带的idle,总是感觉里面的语法提示不出来,经常会因为一些单词打错了,导致程序出错。有什么好办法,或者插件能够让我减少这种错误吗?
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2015-10-18 22:12:13 |显示全部楼层
wcjking331 发表于 2015-10-18 20:39
请问一个问题,我习惯写c,刚开始写python,用的是自带的idle,总是感觉里面的语法提示不出来,经常会因为 ...

装个pycharm之类的
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

200

积分

中级会员

Rank: 3Rank: 3

发表于 2015-12-27 15:50:29 |显示全部楼层
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. from sys import exit

  4. class Bullet:
  5.     def __init__(self):  #初始化成员函数,可是为什么python第49课例子的直接上‘speed = 0’而不用__init__函数初始化呢?
  6.         self.x = 0
  7.         self.y = -1
  8.         self.image = pygame.image.load('f:\plane\sullet.png').convert_alpha()
  9.     def move(self):
  10.         if self.y < 0:
  11.             mouseX,mouseY = pygame.mouse.get_pos()
  12.             self.x = mouseX - self.image.get_width() / 2
  13.             self.y = mouseY - self.image.get_height() / 2
  14.         else:
  15.             self.y -= 5
  16.             
  17. class Bullet_l(Bullet):  #继承了父类Bullet;这个类是为了确定左边子弹的坐标
  18.     def move(self):
  19.         if self.y < 0:
  20.             mouseX,mouseY = pygame.mouse.get_pos()
  21.             self.x = mouseX - self.image.get_width() / 2
  22.             self.y = mouseY - self.image.get_height() / 2
  23.             self.x = self.x - 3.5 * self.image.get_width()
  24.         else:
  25.             self.y -= 5
  26.             
  27. class Bullet_r(Bullet):  #同样继承父类Bullet;为了确定右边子弹的坐标
  28.     def move(self):
  29.         if self.y < 0:
  30.             mouseX,mouseY = pygame.mouse.get_pos()
  31.             self.x = mouseX - self.image.get_width() / 2
  32.             self.y = mouseY - self.image.get_height() / 2
  33.             self.x = self.x + 3.5 * self.image.get_width()
  34.         else:
  35.             self.y -= 5
  36. #启动pygame
  37. pygame.init()

  38. #创建pygame的窗口,设置了窗口尺寸和窗口标题
  39. screen = pygame.display.set_mode((450,800),0,32)
  40. pygame.display.set_caption('Hello World')
  41. background = pygame.image.load('f:\plane\some.jpg')
  42. plane = pygame.image.load('f:\plane\plane.png').convert_alpha()

  43. #创建三个类的对象
  44. bullet = Bullet()
  45. bullet_l = Bullet_l()
  46. bullet_r = Bullet_r()

  47. while True:
  48.     for event in pygame.event.get():
  49.         if event.type == pygame.QUIT:
  50.             pygame.quit()
  51.             exit()
  52.     screen.blit(background,(0,0))

  53.     #确定三个图片(子弹)的坐标
  54.     bullet.move()
  55.     bullet_l.move()
  56.     bullet_r.move()

  57.     #因为这3张图片互不干扰,所以可以不分前后顺序
  58.     screen.blit(bullet.image,(bullet.x,bullet.y))
  59.     screen.blit(bullet.image,(bullet_l.x,bullet_l.y))
  60.     screen.blit(bullet.image,(bullet_r.x,bullet_r.y))

  61.     #感觉飞机的图片并不是完全对称的,我把子弹图片的中心点设置为鼠标坐标,然而子弹竟然不是从飞机头正中射出去的,而是稍微偏左了。
  62.     #左右两边的子弹看起来偏离地更离谱。
  63.     x,y = pygame.mouse.get_pos()
  64.     planeX = x - plane.get_width() / 2
  65.     planeY = y - plane.get_height() / 2
  66.     screen.blit(plane,(planeX,planeY))
  67.     pygame.display.update()
复制代码
回复

使用道具 举报

0

主题

1

好友

273

积分

中级会员

Rank: 3Rank: 3

发表于 2016-2-2 12:15:36 |显示全部楼层
get it~~
回复

使用道具 举报

0

主题

0

好友

34

积分

新手上路

Rank: 1

发表于 2016-3-14 12:32:44 |显示全部楼层
第5课里自己编的情况是 只有按下鼠标的时候才会发射子弹,所以不用面向对象的方式的话可以很轻松的实现
但是这一课里,如果用 crossin给的 class的话,似乎没办法实现按鼠标才发射子弹?
应该怎么办呢?
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2016-3-14 12:44:40 |显示全部楼层
mike90326 发表于 2016-3-14 12:32
第5课里自己编的情况是 只有按下鼠标的时候才会发射子弹,所以不用面向对象的方式的话可以很轻松的实现
但 ...

不是没办法,只是你要改代码。不是让子弹自己循环move
你判断鼠标按着的时候,就创建新的子弹,让它们move,子弹类内部判断,如果高度超出屏幕了,就自己销毁。
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

34

积分

新手上路

Rank: 1

发表于 2016-3-14 12:55:46 |显示全部楼层
crossin先生 发表于 2016-3-14 12:44
不是没办法,只是你要改代码。不是让子弹自己循环move
你判断鼠标按着的时候,就创建新的子弹,让它们mov ...

还是有几点疑问:
1. MOUSEBUTTONDOWN 是一个状态还是一个即时动作呢?从切换图片的那课程来看,应该是一个即时动作。因为如果一直按着鼠标,图片没有不停的切换。
2. 如果在while循环内部套嵌if event.type == pygame.MOUSEBUTTONDOWN 的话,并且把bullet.move()放到if中,应该是只有在鼠标按下的一瞬间才会创建新子弹并移动吧?结果是时间太短显示不出来。
所以还是不大明白应该怎么写哈。。。麻烦crossin了
回复

使用道具 举报

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

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

GMT+8, 2024-3-29 02:31 , Processed in 0.026934 second(s), 21 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部