未加星标

gamingdirectional: Move the enemy ship up and down

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二03 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

Hello and welcome back, in this article we will create a mechanism to move the horizontal moving enemy ship up and down within a certain vertical range. It will be very boring if the horizontal moving enemy can only move from side to side only, thus by including the vertical movement of the enemy ship will improve the quality of the entire game.

In order to move the enemy ship within a certain vertical range we will need to edit the enemy1 class accordingly by including a switch to control the up and down movement of the enemy ship.

from pygame import math as mt from Objectpool import Objectpool class Enemy1(object): def __init__(self, enemy_surface, x, y): self.on = True self.enemy_surface = enemy_surface self.x = x self.y = y self.hit = False self.direction = True self.y_direction = True self.enemy_pos = mt.Vector2(self.x, self.y) self.missile_count = 10 self.missile_timer = 0 self.missile_object_pool = Objectpool(self.missile_count) self.missile_list = [] def update(self): if(self.direction == True): self.x += 0.1 else: self.x -= 0.1 if(self.y >= 160 and self.y_direction == True): self.y -= 0.1 elif (self.y <= 250 and self.y_direction == False): self.y += 0.1 if (self.y < 160): self.y_direction = False elif(self.y > 250): self.y_direction = True self.enemy_pos = mt.Vector2(self.x, self.y) self.missile_update(self.missile_object_pool) def missile_update(self, pool): for item in list(self.missile_list): if (item.on == False): self.missile_list.remove(item) pool.recycle(item) else: item.update() def missile_draw(self, scene): # draw enemy missiles on game scene for item in list(self.missile_list): scene.blit(item.missile_surface, item.missile_pos) def create_enemy_missile(self, enemy_missile_manager): if(self.missile_timer > 300): self.missile_timer = 0 if (self.missile_object_pool.getSize() > 0): enemy_missile_manager.create_missile(self.x + 3, self.y + 100, self.missile_object_pool, self.missile_list) enemy_missile_manager.create_missile(self.x + 50, self.y + 100, self.missile_object_pool, self.missile_list) enemy_missile_manager.create_missile(self.x + 100, self.y + 100, self.missile_object_pool, self.missile_list) else: enemy_missile_manager.create_missile(self.x + 3, self.y + 100, None, self.missile_list) enemy_missile_manager.create_missile(self.x + 50, self.y + 100, None, self.missile_list) enemy_missile_manager.create_missile(self.x + 100, self.y + 100, None, self.missile_list) else: self.missile_timer += 1

After you have edited the above enemy class, the horizontal moving enemy ship will be able to move up and down as shown in the below video.

The up and down movement of the enemy ship

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

代码区博客精选文章
分页:12
转载请注明
本文标题:gamingdirectional: Move the enemy ship up and down
本站链接:https://www.codesec.net/view/628196.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(93)