设为首页收藏本站

Crossin的编程教室

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

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

[复制链接]

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()
复制代码
回复

使用道具 举报

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

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

GMT+8, 2024-5-2 19:08 , Processed in 0.023791 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部