import pygame
import os
import random
from pygame.locals import * # Constants
import math
import sys
import random
pygame.init()
screen=pygame.display.set_mode((1280,700)) #(length,height)
screen_rect=screen.get_rect()
background = pygame.Surface(screen.get_size())
background.fill((255,255,255)) # fill the background white
background = pygame.image.load('stage.png').convert()
Black=(0,0,0)
class Player(pygame.sprite.Sprite):
x = 20
y = 615
def __init__(self):
super().__init__() # calls the parent class allowing sprite to initalize
self.image = pygame.Surface((50,25)) # this is used to create a blank image with the size inputted
self.image.fill((0,0,128)) # fills the blank image with colour
self.rect = self.image.get_rect(topleft =(20,615)) # This place the player at the given position
self.dist = 10
def update(self): # code to make the character move when the arrow keys are pressed
if self.rect.right > 100: # These are to make the player so move constantly
self.rect.y += 1
self.rect.x += 2
if self.rect.bottom == 700:
pygame.quit()
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
self.rect.move_ip(-1,0)
elif keys[K_RIGHT]:
self.rect.move_ip(0.5,0)
elif keys[K_UP]:
self.rect.move_ip(0,-0.5)
elif keys[K_DOWN]:
self.rect.move_ip(0,1)
self.rect.clamp_ip(screen_rect)
#while self.rect == (20,615):
if keys [K_SPACE]:
self.rect = self.image.get_rect(topleft =(100,100))
class Enemy(pygame.sprite.Sprite): # the enemy class which works fine
def __init__(self):
super().__init__()
x = random.randint(50,450)
self.image = pygame.Surface((50,25))
self.image.fill((128,0,0))
self.rect = self.image.get_rect(topleft (300, 50))
self.direction = 0
def update(self):
self.rect.y += 2 if self.direction == 0 else -2
if self.rect.bottom >= 600:
self.direction = 1
if self.rect.top <= 50:
self.direction = 0
clock = pygame.time.Clock() # A clock to limit the frame rate.
player = Player()
enemy = Enemy()
enemy_list = pygame.sprite.Group() # a group where the enemys will be put
sprites = pygame.sprite.Group(player) # The group where evry spirte will be put into
for i in range (5): # creates 5 enemy spirtes
enemy = Enemy() # calls the enemy class
enemy.rect.x = random.randrange(200, 1100) # makes the enemny spawn random
enemy.rect.y = random.randrange(50, 600)
enemy_list.add(enemy) # adds the enemy to the group
sprites.add(enemy)
我得到了这样的代码,敌人会随机移动,然后它们会上下移动,但是由于生成是随机的,所以它们有时会重叠,我想知道如何获得它,以便当它们向上移动时不会重叠和向下 我想知道我是否可以做到,所以当它们产生时它们之间有间隙在x轴上为50,但仍会产生5个敌人
def main(): #my main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
sprites.update()
screen.blit(background, (0, 0))
sprites.draw(screen)
clock.tick(100) # Limit the frame rate to 60 FPS.
pygame.display.flip() #updates the whole screen
#Collison check
player_hit_list = pygame.sprite.spritecollide(player, enemy_list, True)
for enemy in player_hit_list:
pygame.quit()
if __name__ == '__main__':
main()
答案 0 :(得分:0)
尝试这种方法(我故意使代码有些冗长,以试图解释发生了什么事情):
...
number_of_enemies = 5
min_enemy_x = 200
max_enemy_x = 1100
enemy_x_range = max_enemy_x - min_enemy_x # zone in which enemies can spawn
enemy_zone_width = enemy_x_range / number_of_enemies # zone width for each enemy
pixel_buffer = 50 # gap between enemies
for i in range (number_of_enemies): # creates 5 enemy spirtes
enemy = Enemy() # calls the enemy class
min_x = min_enemy_x + enemy_zone_width * i + pixel_buffer / 2 # minimum x at which current enemy can spawn (including buffer)
max_x = min_enemy_x + enemy_zone_width * (i + 1) - pixel_buffer / 2 # maximum x at which current enemy can spawn (including buffer)
enemy.rect.x = random.randrange(min_x, max_x) # makes the enemy spawn random
enemy.rect.y = random.randrange(50, 600)
enemy_list.add(enemy) # adds the enemy to the group
...
请注意,这将在起点和终点产生25像素的缓冲区(因此敌人实际上会在225和1075之间产生),但是您可以调整min_enemy_x
和max_enemy_x
进行补偿或删除第一次和最后一次循环迭代的缓冲。