设为首页收藏本站

Crossin的编程教室

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

【Pygame 第8课】 火力全开

[复制链接]

0

主题

0

好友

10

积分

新手上路

Rank: 1

楼主
发表于 2018-4-2 20:06:31 |显示全部楼层
请问老师:我这样为什么不会有子弹发射出去呀?感觉问题出在bullet.move()里面,但是一用bullet.active就没有子弹了
  1. import pygame
  2. from sys import exit
  3. from alien import Alien
  4. from random import randint
  5. from random import random

  6. class Alien:
  7.     def restart(self):
  8.         self.x = randint(50,350)
  9.         self.y = randint(-200,-50)
  10.         self.speed = random()+1
  11.     def __init__(self):
  12.         self.restart()
  13.         self.image = pygame.image.load('image/alien.bmp')
  14.     def move(self):
  15.         if self.y < 700:
  16.             self.y += self.speed
  17.         else:
  18.             self.restart()

  19. class Bullet():
  20.     def __init__(self):
  21.         self.x = 0
  22.         self.y = 0
  23.         self.image = pygame.image.load('image/bullet.bmp')
  24.         self.active = False
  25.     def restart(self):
  26.         self.active = True
  27.         (mouse_x,mouse_y) = pygame.mouse.get_pos()
  28.         self.x = mouse_x - self.image.get_width()/2
  29.         self.y = mouse_y - self.image.get_height()/2
  30.     def move(self):
  31.         if self.active == True:
  32.             self.y -= 5
  33.         if self.y <0:
  34.             self.active = False

  35. pygame.init()
  36. screen = pygame.display.set_mode((400,700))
  37. pygame.display.set_caption('打飞机')
  38. background = pygame.image.load('image/bg.bmp')
  39. ship = pygame.image.load('image/ship.bmp')
  40. bullet = Bullet()
  41. alien = Alien()
  42. while True:
  43.     for event in pygame.event.get():
  44.         if event.type == pygame.QUIT:
  45.             pygame.quit()
  46.             exit()
  47.         if event.type == pygame.MOUSEBUTTONDOWN:
  48.             background = pygame.image.load('image/bg2.bmp')
  49.     (x,y) = pygame.mouse.get_pos()
  50.     x -= (ship.get_width()/2)
  51.     y -= (ship.get_width()/2)
  52.     bullet.move()
  53.     alien.move()
  54.     screen.blit(background,(0,0))
  55.     screen.blit(ship,(x,y))
  56.     screen.blit(bullet.image,(bullet.x,bullet.y))
  57.     screen.blit(alien.image,(alien.x,alien.y))
  58.     pygame.display.update()
复制代码
回复

使用道具 举报

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

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

GMT+8, 2024-5-8 18:27 , Processed in 0.016737 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部