Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame-及时随机丢弃敌人_Python_Pygame - Fatal编程技术网

Python Pygame-及时随机丢弃敌人

Python Pygame-及时随机丢弃敌人,python,pygame,Python,Pygame,我正试图用Python和Pygame设计我的第一个游戏,以提高我的编码技能 基本上,玩家有4个圆圈,目标是防止立方体(或敌人)到达屏幕中央,用键盘箭头杀死他们 游戏运行良好:圆圈移动完美,立方体随着关卡的提升而加快,评分系统正常,等等 我想不出的最后一步是让立方体更随意地掉落,否则太容易了。当前,当一个立方体被杀死时,它会立即出现在屏幕的4个边缘之一。我想出了如何随机定位立方体,但计时不起作用 我尝试了Python睡眠,但它冻结了屏幕。我尝试添加一行以“delay=random.random()

我正试图用Python和Pygame设计我的第一个游戏,以提高我的编码技能

基本上,玩家有4个圆圈,目标是防止立方体(或敌人)到达屏幕中央,用键盘箭头杀死他们

游戏运行良好:圆圈移动完美,立方体随着关卡的提升而加快,评分系统正常,等等

我想不出的最后一步是让立方体更随意地掉落,否则太容易了。当前,当一个立方体被杀死时,它会立即出现在屏幕的4个边缘之一。我想出了如何随机定位立方体,但计时不起作用


我尝试了Python睡眠,但它冻结了屏幕。我尝试添加一行以“delay=random.random()”指定延迟,并添加“delay我建议使用计时器事件。使用可重复创建。例如:

毫秒\u延迟=3000
敌人\u繁殖\u事件=pygame.USEREVENT+1
pygame.time.set_计时器(敌人产卵事件,毫秒延迟)
注意,在pygame中可以定义客户事件。每个事件都需要一个唯一的id。用户事件的id必须从
pygame.USEREVENT
开始。在这种情况下,
pygame.USEREVENT+1
是计时器事件的事件id,计时器事件会产生敌人

在事件循环中发生事件时创建新敌人:

毫秒\u延迟=3000
敌人\u繁殖\u事件=pygame.USEREVENT+1
pygame.time.set_计时器(敌人产卵事件,毫秒延迟)
虽然游戏尚未结束:
# [...]
#记分和生命积分系统
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
sys.exit()
如果event.type==敌方产卵事件和len(所有敌方)<敌方数量:
所有敌人。追加(敌人(int(屏幕宽度/2),0,颜色=黄色,位置=“顶部”))
对于所有敌人中的e:
e、 随机化
时间计数器=0

谢谢Rabbit76。我更新了我的代码,现在它以固定的延迟工作,这比以前好多了。我试图让它有点随机,所以我添加了毫秒\u delay=random.randint(50010000)在while循环中导入了随机包,但它仍然不起作用…我做了如下操作@rabbi76:import pygame,random from pygame.locals import*def timerFunc():print(“Timer CallBack”)pygame.init()毫秒延迟=2000 pygame.time.set_Timer(USEREVENT+1,毫秒延迟)而pygame.event.get()中的事件的1:millizes\u delay=random.randint(5005000):if event.type==USEREVENT+1:timerFunc()if event.type==QUIT:break@Nathan“还是不行“-什么不起作用?当然,您必须在计时器事件回调之外启动一次时间,并在
timerFunc
中连续调用
pygame.init()
,这是一个糟糕的想法。ait现在可以工作了。谢谢如果event.type==USEREVENT+1和len(所有敌人) # ---------- Packages and Inits ---------- import pygame, random, math, sys pygame.init() # ---------- Settings ---------- SCREEN_WIDTH = 600 SCREEN_HEIGHT = 600 FPS = 60 SPEED = 1 CIRCLE_RADIUS = 50 ENEMY_SIZE = 40 # Colors RED = (255,000,000) BLUE = (000,000,255) YELLOW = (255,255,000) GREEN = (000,128,000) BLACK = (000,000,000) # ---------- Classes ---------- class Enemies: def __init__( self, x, y, size=ENEMY_SIZE, thick=5, color=BLUE, speed=1, position="top"): self.rect = pygame.Rect(0, 0, size, size) if ( x == 0 and y == 0 ): self.randomise() self.rect.centerx = x self.rect.centery = y self.size = size self.thick = thick self.color = color self.speed = speed self.calcDirection() self.position = position def calcDirection( self ): self.x_float = 1.0 * self.rect.centerx self.y_float = 1.0 * self.rect.centery # Determine direction vector from (x,y) to the centre of the screen self.position_vector = pygame.math.Vector2( self.x_float, self.y_float ) self.velocity_vector = pygame.math.Vector2( SCREEN_WIDTH/2 - self.x_float, SCREEN_HEIGHT/2 - self.y_float ) self.velocity_vector = self.velocity_vector.normalize() def update( self ): x_delta = self.speed * self.velocity_vector[0] y_delta = self.speed * self.velocity_vector[1] self.x_float += x_delta self.y_float += y_delta self.rect.centerx = int( self.x_float ) self.rect.centery = int( self.y_float ) def draw( self, screen): pygame.draw.rect(screen, self.color, self.rect ) def reachedPoint( self, x, y ): return self.rect.collidepoint( x, y ) def randomise( self ): self.rect.centerx = SCREEN_WIDTH//2 self.rect.centery = SCREEN_HEIGHT//2 side = random.randint( 0, 4 ) if ( side == 0 ): self.rect.centery = SCREEN_HEIGHT self.color = GREEN self.position= "bot" elif ( side == 1 ): self.rect.centery = 0 self.color = YELLOW self.position= "top" elif ( side == 2 ): self.rect.centerx = 0 self.color = BLUE self.position= "left" else: self.rect.centerx = SCREEN_WIDTH self.color = RED self.position= "right" self.calcDirection() def set_speed( self, score): if score < 25 : self.speed = 0.5 elif score < 50 : self.speed = 0.75 elif score < 100: self.speed = 1 elif score < 250: self.speed = 1.25 elif score < 500: self.speed = 1.5 else: self.speed = 2 return self.speed class Circle: def __init__(self, x, y, radius=CIRCLE_RADIUS, thick=5, color=BLUE, speed=SPEED, position="top"): self.rect = pygame.Rect(0, 0, 2*radius, 2*radius) self.rect.centerx = x self.rect.centery = y self.radius = radius self.thick = thick self.color = color self.speed = speed self.position = position if speed >= 0: self.directionX = 'right' self.direction = 'up' else: self.directionX = 'left' self.direction = 'down' def draw(self, screen): pygame.draw.circle(screen, self.color, self.rect.center, self.radius, self.thick) def swing(self): if self.position == "top": self.rect.y -= self.speed if self.rect.top <= 0 and self.direction == 'up': self.direction = 'down' self.speed = -self.speed elif self.rect.bottom > int(SCREEN_HEIGHT/2) - self.radius and self.direction == 'down': self.direction = 'up' self.speed = -self.speed if self.position == "bot": self.rect.y -= self.speed if self.rect.top < int(SCREEN_HEIGHT/2) + self.radius and self.direction == 'up': self.direction = 'down' self.speed = -self.speed elif self.rect.bottom >= SCREEN_HEIGHT and self.direction == 'down': self.direction = 'up' self.speed = -self.speed if self.position == "left": self.rect.x -= self.speed if self.rect.right > int(SCREEN_WIDTH/2) - self.radius and self.directionX == 'left': self.directionX = 'right' self.speed = -self.speed elif self.rect.left <= 0 and self.directionX == 'right': self.directionX = 'left' self.speed = -self.speed if self.position == "right": self.rect.x -= self.speed if self.rect.left < int(SCREEN_WIDTH/2) + self.radius and self.directionX == 'right': self.directionX = 'left' self.speed = -self.speed elif self.rect.right >= SCREEN_WIDTH and self.directionX == 'left': self.directionX = 'right' self.speed = -self.speed def isCollision(self, enemyX, enemyY, circleX, circleY): distance = math.sqrt((math.pow(enemyX-circleX,2))+(math.pow(enemyY-circleY,2))) if distance < 65: return True else: return False # ---------- Other Functions ---------- def set_number_of_enemies(score): if score < 25 : number_of_enemies = 3 elif score < 50 : number_of_enemies = 4 elif score < 100: number_of_enemies = 5 elif score < 250: number_of_enemies = 6 elif score < 500: number_of_enemies = 7 else: number_of_enemies = 8 return number_of_enemies # ---------- Main ---------- def main(): # Settings screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) screen_rect = screen.get_rect() myFont = pygame.font.SysFont("monospace", 25) clock = pygame.time.Clock() game_over = False # Variables lifes = 5 score = 0 number_of_enemies = 3 # We create an empty list of enemies, as we want them to drop randomly all_enemies = [] # Start with 4 circles all_circles = [ Circle ( screen_rect.centerx , screen_rect.centery - 2*CIRCLE_RADIUS , position = "top" ), Circle ( screen_rect.centerx , screen_rect.centery + 2*CIRCLE_RADIUS , position = "bot" ), Circle ( screen_rect.centerx + 2*CIRCLE_RADIUS , screen_rect.centery , position = "right"), Circle ( screen_rect.centerx - 2*CIRCLE_RADIUS , screen_rect.centery , position = "left" )] while not game_over: screen.fill(BLACK) # This has to be inside the while not game_over loop time_counter = clock.tick() print(time_counter) # Circles for c in all_circles: c.draw(screen) # Place circles on the screen c.swing() # Move circles from center to edges, and from edges to center, back and forth # Set number of enemies number_of_enemies = set_number_of_enemies(score) # Get the number of enemies we want, based on the score if len(all_enemies) < number_of_enemies and time_counter > 3000: # Add if necessary all_enemies.append(Enemies(int(SCREEN_WIDTH/2), 0, color = YELLOW, position = "top")) # We add a top enemy, but we randomise it right after for e in all_enemies: e.randomise() time_counter = 0 # Enemies for e in all_enemies: e.draw(screen) # Place enemies on the screen e.set_speed(score) # Set speed difficulty for enemies e.update() # Move enemies from the edges of the screen towards the center if ( e.reachedPoint( SCREEN_WIDTH//2, SCREEN_HEIGHT//2 ) ): # If the enemy reaches the middle, you lose a lifepoint and a new enemy is generated lifes -=1 e.randomise() # Scoring and lifepoints systems for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() for c in all_circles: if event.type == pygame.KEYDOWN: # LEFT if event.key == pygame.K_LEFT and c.position == "left": hits = [e for e in all_enemies if c.isCollision(e.rect.centerx,e.rect.centery,c.rect.centerx,c.rect.centery)] if not hits: lifes -=1 for e in hits: if len(hits) == 1: score +=1 if len(hits) == 2: score +=5/len(hits) if len(hits) == 3: score +=10/len(hits) e.randomise() # RIGHT if event.key == pygame.K_RIGHT and c.position == "right": hits = [e for e in all_enemies if c.isCollision(e.rect.centerx,e.rect.centery,c.rect.centerx,c.rect.centery)] if not hits: lifes -=1 for e in hits: if len(hits) == 1: score +=1 if len(hits) == 2: score +=5/len(hits) if len(hits) == 3: score +=10/len(hits) e.randomise() # TOP if event.key == pygame.K_UP and c.position == "top": hits = [e for e in all_enemies if c.isCollision(e.rect.centerx,e.rect.centery,c.rect.centerx,c.rect.centery)] if not hits: lifes -=1 for e in hits: if len(hits) == 1: score +=1 if len(hits) == 2: score +=5/len(hits) if len(hits) == 3: score +=10/len(hits) e.randomise() # BOT if event.key == pygame.K_DOWN and c.position == "bot": hits = [e for e in all_enemies if c.isCollision(e.rect.centerx,e.rect.centery,c.rect.centerx,c.rect.centery)] if not hits: lifes -=1 for e in hits: if len(hits) == 1: score +=1 if len(hits) == 2: score +=5/len(hits) if len(hits) == 3: score +=10/len(hits) e.randomise() # Game Over condition if lifes == 0: game_over = True # Score / Lifes / Number of Enemies print_lifes = myFont.render("Lifes:" + str(round(lifes)), 1, RED) screen.blit(print_lifes, (10, SCREEN_HEIGHT-50)) print_score = myFont.render("Score:" + str(round(score)), 1, RED) screen.blit(print_score, (10, 10)) print_enemies = myFont.render("# of Enemies:" + str(round(number_of_enemies)), 1, RED) screen.blit(print_enemies, (10, 60)) pygame.display.update() clock.tick(FPS) main() pygame.quit()