def __init__(self,screen): self.screen=screen # self.ai_xin=pygame.image.load("image/爱心模型2.png") # self.ai_xin_rect=ai_xin.get_rect() # self.ai_xin_rect.centerx=200 # self.ai_xin_rect.centery=200 # self.screen.blit(ai_xin,ai_xin_rect) self.ai_xin_group=pygame.sprite.OrderedUpdates() self.game_ai_xin=GameSprite('image/aixin.png',7) self.ai_xin_group.add(self.game_ai_xin) self.frame=0 self.next_frame=pygame.time.get_ticks()
def __init__(self, screen, x, y): super(Spirit, self).__init__() # self.image=pygame.image.load("image/spirit.png") # self.rect=self.image.get_rect() self.screen = screen self.pos_x = x self.pos_y = y self.spirit_group = pygame.sprite.OrderedUpdates() #按添加顺序绘制sprite类 self.game_spirit = GameSprite('image/spirit_sprite.png', 16) self.game_spirit.move(self.pos_x, self.pos_y) self.spirit_group.add(self.game_spirit) self.frame = 0 self.next_frame = pygame.time.get_ticks() self.random_direction = 1 self.speed = 0.3
def __init__(self, screen, gf): self.screen = screen # self.rect=self.image.get_rect() #把图片设置为矩形,把图片放入碰撞矩形 # self.screen_rect=screen.get_rect() # self.rect.centerx=self.screen_rect.centerx # self.rect.centery=self.screen_rect.centery self.gf = gf self.jack_group = pygame.sprite.OrderedUpdates() #按添加顺序绘制sprite类 self.game_jack = GameSprite('image/jack_sprite.png', 16) self.game_jack.move(600, 400) self.jack_group.add(self.game_jack) self.frame = 0 self.next_frame = pygame.time.get_ticks() self.speed = 0.9 self.direction = 4 self.wd = False
def __init__(self, screen): self.screen = screen self.hyjq_group = pygame.sprite.OrderedUpdates() self.game_hyjq = GameSprite('image/attack_1.png', 5) self.hyjq_group.add(self.game_hyjq) self.frame = 0 self.next_frame = pygame.time.get_ticks() self.frame_2 = 0 self.next_frame_2 = pygame.time.get_ticks() self.next_frame_4 = pygame.time.get_ticks() self.next_frame_5 = pygame.time.get_ticks() self.next_frame_5 += 7000 self.frame_3 = 0 self.next_frame_3 = pygame.time.get_ticks() self.fq_group = pygame.sprite.OrderedUpdates() self.game_fq = GameSprite('image/attack_2.png', 6) self.fq_group.add(self.game_fq) self.js_group = pygame.sprite.OrderedUpdates() self.game_js = GameSprite('image/defense_1.png', 2) self.js_group.add(self.game_js) self.jq_speed = 0 self.fq_speed = 0 self.angle = 0 self.angle_pd = 0 self.t = 0 self.r = 0 self.fq_jt = False
class Jack(): def __init__(self, screen, gf): self.screen = screen # self.rect=self.image.get_rect() #把图片设置为矩形,把图片放入碰撞矩形 # self.screen_rect=screen.get_rect() # self.rect.centerx=self.screen_rect.centerx # self.rect.centery=self.screen_rect.centery self.gf = gf self.jack_group = pygame.sprite.OrderedUpdates() #按添加顺序绘制sprite类 self.game_jack = GameSprite('image/jack_sprite.png', 16) self.game_jack.move(600, 400) self.jack_group.add(self.game_jack) self.frame = 0 self.next_frame = pygame.time.get_ticks() self.speed = 0.9 self.direction = 4 self.wd = False def blitme(self): # self.screen.blit(self.image,self.rect) self.jack_group.draw(self.screen) def jack_move(self, background, spirits, gems, nests): aixin = Ai_xin(self.screen) if pygame.time.get_ticks() > self.next_frame: self.frame = (self.frame + 1) % 4 self.next_frame += 200 if self.gf.key_pressed(pygame.K_RIGHT): self.game_jack.change_image(self.frame + 8) background.move(-self.speed, 0) self.jack_group.draw(self.screen) spirits.update(True, -self.speed, 0) gems.update(True, -self.speed, 0) nests.update(True, -self.speed, 0) self.direction = 1 elif self.gf.key_pressed(pygame.K_LEFT): self.game_jack.change_image(self.frame + 4) background.move(self.speed, 0) self.jack_group.draw(self.screen) spirits.update(True, self.speed, 0) gems.update(True, self.speed, 0) nests.update(True, self.speed, 0) self.direction = 2 elif self.gf.key_pressed(pygame.K_UP): self.game_jack.change_image(self.frame + 12) background.move(0, self.speed) self.jack_group.draw(self.screen) spirits.update(True, 0, self.speed) gems.update(True, 0, self.speed) nests.update(True, 0, self.speed) self.direction = 3 elif self.gf.key_pressed(pygame.K_DOWN): self.game_jack.change_image(self.frame) background.move(0, -self.speed) self.jack_group.draw(self.screen) spirits.update(True, 0, -self.speed) gems.update(True, 0, -self.speed) nests.update(True, 0, -self.speed) self.direction = 4 else: background.move(0, 0) self.jack_group.draw(self.screen) def jack_attack(self, attack): if self.gf.key_pressed(pygame.K_z): if self.direction == 1: attack.angle = 0 attack.hyjq_gj(630, 400) elif self.direction == 2: attack.angle = 180 attack.hyjq_gj(580, 400) elif self.direction == 3: attack.angle = 90 attack.hyjq_gj(600, 370) elif self.direction == 4: attack.angle = 270 attack.hyjq_gj(600, 430) if self.gf.key_pressed(pygame.K_x): if self.direction == 1: attack.angle = 0 attack.fq_gj(630, 400) elif self.direction == 2: attack.angle = 180 attack.fq_gj(580, 400) elif self.direction == 3: attack.angle = 90 attack.fq_gj(600, 370) elif self.direction == 4: attack.angle = 270 attack.fq_gj(600, 430) else: if self.direction == 1: attack.angle = 0 attack.fq_gj_fs() elif self.direction == 2: attack.angle = 180 attack.fq_gj_fs() elif self.direction == 3: attack.angle = 90 attack.fq_gj_fs() elif self.direction == 4: attack.angle = 270 attack.fq_gj_fs() if self.gf.key_pressed(pygame.K_c): attack.js_fy() self.wd = True else: self.wd = False
import pygame from game_sprite import GameSprite # create background pic pygame.init() screen = pygame.display.set_mode((480, 700)) bg = pygame.image.load("./images/xingyun1.jpg") screen.blit(bg, (0, 0)) # create my air hreo = pygame.image.load("./images/hero0.png") screen.blit(hreo, (150, 500)) # create Enemy air enemy = GameSprite("./images/enemy2.png") enemy1 = GameSprite("./images/enemy2.png", 4) enemyGroup = pygame.sprite.Group(enemy, enemy1) clock = pygame.time.Clock() plane_init_place = pygame.Rect(150, 300, 102, 126) while True: # 用于指定循环体内的代码执行次数,即帧数 clock.tick(60) for e in pygame.event.get(): if e.type == pygame.QUIT: print("Quit-------------------------------------") pygame.quit() exit() # update air's place plane_init_place.y -= 5 if plane_init_place.y <= 0: plane_init_place.y = 700
class Attack(): def __init__(self, screen): self.screen = screen self.hyjq_group = pygame.sprite.OrderedUpdates() self.game_hyjq = GameSprite('image/attack_1.png', 5) self.hyjq_group.add(self.game_hyjq) self.frame = 0 self.next_frame = pygame.time.get_ticks() self.frame_2 = 0 self.next_frame_2 = pygame.time.get_ticks() self.next_frame_4 = pygame.time.get_ticks() self.next_frame_5 = pygame.time.get_ticks() self.next_frame_5 += 7000 self.frame_3 = 0 self.next_frame_3 = pygame.time.get_ticks() self.fq_group = pygame.sprite.OrderedUpdates() self.game_fq = GameSprite('image/attack_2.png', 6) self.fq_group.add(self.game_fq) self.js_group = pygame.sprite.OrderedUpdates() self.game_js = GameSprite('image/defense_1.png', 2) self.js_group.add(self.game_js) self.jq_speed = 0 self.fq_speed = 0 self.angle = 0 self.angle_pd = 0 self.t = 0 self.r = 0 self.fq_jt = False def blitme(self, x, y): self.game_hyjq.move(x, y) self.hyjq_group.draw(self.screen) def hyjq_gj(self, x, y): t = x r = y if pygame.time.get_ticks() > self.next_frame: self.frame = (self.frame + 1) % 5 self.jq_speed += 8 self.next_frame += 120 t = x + self.jq_speed if self.angle == 0 else t t = x - self.jq_speed if self.angle == 180 else t r = y + self.jq_speed if self.angle == 270 else r r = y - self.jq_speed if self.angle == 90 else r self.game_hyjq.move(t, r) self.game_hyjq.change_image(self.frame) self.game_hyjq.retate(self.angle) self.hyjq_group.draw(self.screen) if self.frame == 0: self.jq_speed = 0 self.next_frame = pygame.time.get_ticks() def fq_gj(self, x, y): if self.fq_jt == False: self.t = x self.r = y if self.angle == 0: self.angle_pd = 1 elif self.angle == 180: self.angle_pd = 2 elif self.angle == 270: self.angle_pd = 3 elif self.angle == 90: self.angle_pd = 4 self.next_frame_4 = pygame.time.get_ticks() self.next_frame_5 = self.next_frame_4 if self.frame_2 < 5: if pygame.time.get_ticks() > self.next_frame_2: self.frame_2 = (self.frame_2 + 1) % 6 self.next_frame_2 += 800 self.game_fq.move(self.t, self.r) self.game_fq.change_image(self.frame_2) self.fq_group.draw(self.screen) self.fq_speed = 0 def fq_gj_fs(self): if self.frame_2 >= 5: self.fq_jt = True t = self.t r = self.r t = self.t + self.fq_speed if self.angle_pd == 1 else t t = self.t - self.fq_speed if self.angle_pd == 2 else t r = self.r + self.fq_speed if self.angle_pd == 3 else r r = self.r - self.fq_speed if self.angle_pd == 4 else r if pygame.time.get_ticks() > self.next_frame_4: self.fq_speed += 7 self.next_frame_4 += 500 if self.next_frame_4 - self.next_frame_5 >= 8000: self.next_frame_2 = pygame.time.get_ticks() self.frame_2 = 0 self.game_fq.move(t, r) self.fq_group.draw(self.screen) else: self.fq_speed = 0 # self.frame_2=0 self.fq_jt = False def js_fy(self): if pygame.time.get_ticks() > self.next_frame_3: self.frame_3 = (self.frame_3 + 1) % 2 self.next_frame_3 += 300 self.game_js.move(600, 400) self.game_js.change_image(self.frame_3) self.js_group.draw(self.screen)
class Spirit(Sprite): def __init__(self, screen, x, y): super(Spirit, self).__init__() # self.image=pygame.image.load("image/spirit.png") # self.rect=self.image.get_rect() self.screen = screen self.pos_x = x self.pos_y = y self.spirit_group = pygame.sprite.OrderedUpdates() #按添加顺序绘制sprite类 self.game_spirit = GameSprite('image/spirit_sprite.png', 16) self.game_spirit.move(self.pos_x, self.pos_y) self.spirit_group.add(self.game_spirit) self.frame = 0 self.next_frame = pygame.time.get_ticks() self.random_direction = 1 self.speed = 0.3 def update(self, bg_move=False, x=0, y=0, time=0, next_time=0): if (bg_move): self.pos_x += x self.pos_y += y self.game_spirit.move(self.pos_x, self.pos_y) self.spirit_group.draw(self.screen) else: if pygame.time.get_ticks() > self.next_frame: self.frame = (self.frame + 1) % 4 self.next_frame += 300 self.random_direction = random.randint(1, 40) # if time < next_time: # self.random_direction >=1 and self.random_direction <=10 # self.game_spirit.change_image(self.frame+8) # self.pos_x += self.speed # self.game_spirit.move(self.pos_x,self.pos_y) # self.spirit_group.draw(self.screen) if self.random_direction >= 1 and self.random_direction <= 10: self.game_spirit.change_image(self.frame + 8) self.pos_x += self.speed self.game_spirit.move(self.pos_x, self.pos_y) self.spirit_group.draw(self.screen) # self.rect.centerx += 1 elif self.random_direction >= 11 and self.random_direction <= 20: self.game_spirit.change_image(self.frame + 4) # self.rect.centerx -= 1 self.pos_x -= self.speed self.game_spirit.move(self.pos_x, self.pos_y) self.spirit_group.draw(self.screen) elif self.random_direction >= 21 and self.random_direction <= 30: self.game_spirit.change_image(self.frame) # self.rect.centery += 1 self.pos_y += self.speed self.game_spirit.move(self.pos_x, self.pos_y) self.spirit_group.draw(self.screen) elif self.random_direction >= 31 and self.random_direction <= 40: self.game_spirit.change_image(self.frame + 12) # self. rect.centery -= 1 self.pos_y -= self.speed self.game_spirit.move(self.pos_x, self.pos_y) self.spirit_group.draw(self.screen)