import random
import time
import pygame
import sys
import plane
import enemy
def key_control(hero):
for shi_jian in pygame.event.get():
if shi_jian.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif shi_jian.type == pygame.KEYDOWN:
if shi_jian.key == pygame.K_a:
hero.move_left()
if shi_jian.key == pygame.K_d:
hero.move_right()
if shi_jian.key == pygame.K_w:
hero.move_up()
if shi_jian.key == pygame.K_s:
hero.move_down()
if shi_jian.key == pygame.K_f:
hero.fire()
def main():
pygame.init()
window = pygame.display.set_mode((400, 600))
pygame.display.set_caption("星球大戰")
background = pygame.image.load("圖片/background.png")
hero = plane.plane(window)
bad_egg = enemy.enemy(window)
pygame.key.set_repeat(1, 1)
while True:
key_control(hero)
window.blit(background, (0, 0))
hero.show()
bad_egg.show()
print(hero.x,hero.y)
print(bad_egg.x,bad_egg.y)
if hero.x < bad_egg.x<hero.x+120 :
hero.boom()
pygame.display.update()
pygame.quit()
if __name__ == '__main__':
main()
import pygame
import bullet
import sys
import time
class plane():
def __init__(self, window):
self.x = 240
self.y = 426
self.skin = pygame.image.load("圖片/hero1.png")
self.window = window
self.biu = []
self.isboom = False
self.boom_picture = []
def join_boom_picture(self):
self.boom_picture.append(pygame.image.load("圖片/hero_blowup_n1.png"))
self.boom_picture.append(pygame.image.load("圖片/hero_blowup_n2.png"))
self.boom_picture.append(pygame.image.load("圖片/hero_blowup_n3.png"))
self.boom_picture.append(pygame.image.load("圖片/hero_blowup_n4.png"))
def show(self):
if self.isboom:
for i in range(4):
self.window.blit(self.boom_picture[i], (self.x, self.y))
pygame.display.update()
time.sleep(0.1)
pygame.quit()
sys.exit()
else:
self.window.blit(self.skin, (self.x, self.y))
for zidan in self.biu:
zidan.show()
zidan.move()
print(zidan.x, zidan.y)
zidan.yuejie()
if zidan.cross:
self.biu.remove(zidan)
def move_left(self):
self.x -= 10
if self.x < -50:
self.x = -50
def move_right(self):
self.x += 10
if self.x > 350:
self.x = 350
def move_up(self):
self.y -= 10
if self.y < -124:
self.y = 600
def move_down(self):
self.y += 10
if self.y > 600:
self.y = -124
def fire(self):
for i in range(1,100,20):
self.biu.append(bullet.bullet(self.window, self.x + i, self.y))
self.biu.append(bullet.bullet(self.window, self.x + 50+i, self.y))
def boom(self):
self.isboom = True
self.join_boom_picture()