敌人的产卵每次都在同一个地方产卵
Spawning of the enemy spawns at the same place every time
你可以在这个问题上看到我的问题:
.
看到答案后,我将函数更改为:
def spawn():
global blacklisted_numbers
blacklisted_numbers = [] # this might need to be global if you want to use
# this function multiple times
random_locations = [(100, 440), (350, 440), (600, 440), (100, 260), (350, 260),
(600, 260), (100, 80), (350, 80), (600, 80)]
number = choice(random_locations)
while number in blacklisted_numbers:
number = choice(random_locations)
blacklisted_numbers.append(number)
print(number)
print(len(blacklisted_numbers))
print(len(random_locations))
pos1 = number[0]
pos2 = number[1]
print(pos1, pos2)
global final_pos
final_pos = (pos1 + 65)
global final_pos_2
final_pos_2 = (pos2 - 7)
global mole
mole = pygame.image.load('small_mole.png')
global mole_rect
mole_rect = mole.get_rect(center=(final_pos, final_pos_2))
screen.blit(mole, mole_rect)
仍然没有解决。
这是全部代码:
import pygame
from sys import exit
import random
from random import choice
pygame.init()
molepos1 = 0
molepos2 = 0
looptime = 8
def spawn():
global blacklisted_numbers
blacklisted_numbers = [] # this might need to be global if you want to use
# this function multiple times
random_locations = [(100, 440), (350, 440), (600, 440), (100, 260), (350, 260),
(600, 260), (100, 80), (350, 80), (600, 80)]
number = choice(random_locations)
while number in blacklisted_numbers:
number = choice(random_locations)
blacklisted_numbers.append(number)
print(number)
print(len(blacklisted_numbers))
print(len(random_locations))
pos1 = number[0]
pos2 = number[1]
print(pos1, pos2)
global final_pos
final_pos = (pos1 + 65)
global final_pos_2
final_pos_2 = (pos2 - 7)
global mole
mole = pygame.image.load('small_mole.png')
global mole_rect
mole_rect = mole.get_rect(center=(final_pos, final_pos_2))
screen.blit(mole, mole_rect)
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption(' Whack a mole')
icon = pygame.image.load('whack-a-mole-icon.png')
pygame.display.set_icon(icon)
# the screen and the game
screen.fill((0, 255, 0))
background = pygame.image.load('green-grass-background-2036900.jpg').convert_alpha()
score = 0
spawn()
# everything releated to the mole with the mole of course
mole = pygame.image.load('small_mole.png')
mole_home = pygame.image.load('land.png')
mole_home = pygame.transform.scale(mole_home, (130, 80))
mole_rect = mole.get_rect(center=(final_pos, final_pos_2))
# hammer
hammer = pygame.image.load('hammer.png')
hammer2 = pygame.image.load('hammer2.png')
hammer3 = pygame.image.load('hammer3.png')
whyamiahammer = hammer2
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
click = pygame.mouse.get_pressed()
if event.button == 1:
whyamiahammer = hammer3
collision = pygame.Rect.colliderect(hammer_rect, mole_rect)
if collision == True:
spawn()
screen.blit(mole, mole_rect)
score += 1
print('Score was increased by one ')
if event.type == pygame.MOUSEBUTTONUP:
whyamiahammer = hammer2
hammerx, hammery = pygame.mouse.get_pos()
hammer_rect = hammer3.get_rect(center=(hammerx, hammery))
screen.blit(background, (0, 0))
screen.blit(mole,mole_rect)
screen.blit(mole_home, (100, 440))
screen.blit(mole_home, (350, 440))
screen.blit(mole_home, (600, 440))
screen.blit(mole_home, (100, 260))
screen.blit(mole_home, (350, 260))
screen.blit(mole_home, (600, 260))
screen.blit(mole_home, (100, 80))
screen.blit(mole_home, (350, 80))
screen.blit(mole_home, (600, 80))
screen.blit(whyamiahammer, ((hammerx) - 61, (hammery - 73)))
pygame.display.update()
您需要在 spawn
函数之前初始化 blacklisted_numbers
。当所有随机位置都在列表中时重置列表:
blacklisted_numbers = []
def spawn():
global blacklisted_numbers
random_locations = [(100, 440), (350, 440), (600, 440), (100, 260), (350, 260), (600, 260), (100, 80), (350, 80),
(600, 80)]
if len(blacklisted_numbers) == len(random_locations):
blacklisted_numbers = []
# [...]
或者您可以从 random_locations
中删除项目:
random_locations = []
def spawn():
global random_locations
if not random_locations:
random_locations = [(100, 440), (350, 440), (600, 440), (100, 260), (350, 260), (600, 260), (100, 80), (350, 80),
(600, 80)]
number = choice(random_locations)
random_locations.remove(number)
# [...]
你可以在这个问题上看到我的问题:
看到答案后,我将函数更改为:
def spawn():
global blacklisted_numbers
blacklisted_numbers = [] # this might need to be global if you want to use
# this function multiple times
random_locations = [(100, 440), (350, 440), (600, 440), (100, 260), (350, 260),
(600, 260), (100, 80), (350, 80), (600, 80)]
number = choice(random_locations)
while number in blacklisted_numbers:
number = choice(random_locations)
blacklisted_numbers.append(number)
print(number)
print(len(blacklisted_numbers))
print(len(random_locations))
pos1 = number[0]
pos2 = number[1]
print(pos1, pos2)
global final_pos
final_pos = (pos1 + 65)
global final_pos_2
final_pos_2 = (pos2 - 7)
global mole
mole = pygame.image.load('small_mole.png')
global mole_rect
mole_rect = mole.get_rect(center=(final_pos, final_pos_2))
screen.blit(mole, mole_rect)
仍然没有解决。
这是全部代码:
import pygame
from sys import exit
import random
from random import choice
pygame.init()
molepos1 = 0
molepos2 = 0
looptime = 8
def spawn():
global blacklisted_numbers
blacklisted_numbers = [] # this might need to be global if you want to use
# this function multiple times
random_locations = [(100, 440), (350, 440), (600, 440), (100, 260), (350, 260),
(600, 260), (100, 80), (350, 80), (600, 80)]
number = choice(random_locations)
while number in blacklisted_numbers:
number = choice(random_locations)
blacklisted_numbers.append(number)
print(number)
print(len(blacklisted_numbers))
print(len(random_locations))
pos1 = number[0]
pos2 = number[1]
print(pos1, pos2)
global final_pos
final_pos = (pos1 + 65)
global final_pos_2
final_pos_2 = (pos2 - 7)
global mole
mole = pygame.image.load('small_mole.png')
global mole_rect
mole_rect = mole.get_rect(center=(final_pos, final_pos_2))
screen.blit(mole, mole_rect)
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption(' Whack a mole')
icon = pygame.image.load('whack-a-mole-icon.png')
pygame.display.set_icon(icon)
# the screen and the game
screen.fill((0, 255, 0))
background = pygame.image.load('green-grass-background-2036900.jpg').convert_alpha()
score = 0
spawn()
# everything releated to the mole with the mole of course
mole = pygame.image.load('small_mole.png')
mole_home = pygame.image.load('land.png')
mole_home = pygame.transform.scale(mole_home, (130, 80))
mole_rect = mole.get_rect(center=(final_pos, final_pos_2))
# hammer
hammer = pygame.image.load('hammer.png')
hammer2 = pygame.image.load('hammer2.png')
hammer3 = pygame.image.load('hammer3.png')
whyamiahammer = hammer2
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
click = pygame.mouse.get_pressed()
if event.button == 1:
whyamiahammer = hammer3
collision = pygame.Rect.colliderect(hammer_rect, mole_rect)
if collision == True:
spawn()
screen.blit(mole, mole_rect)
score += 1
print('Score was increased by one ')
if event.type == pygame.MOUSEBUTTONUP:
whyamiahammer = hammer2
hammerx, hammery = pygame.mouse.get_pos()
hammer_rect = hammer3.get_rect(center=(hammerx, hammery))
screen.blit(background, (0, 0))
screen.blit(mole,mole_rect)
screen.blit(mole_home, (100, 440))
screen.blit(mole_home, (350, 440))
screen.blit(mole_home, (600, 440))
screen.blit(mole_home, (100, 260))
screen.blit(mole_home, (350, 260))
screen.blit(mole_home, (600, 260))
screen.blit(mole_home, (100, 80))
screen.blit(mole_home, (350, 80))
screen.blit(mole_home, (600, 80))
screen.blit(whyamiahammer, ((hammerx) - 61, (hammery - 73)))
pygame.display.update()
您需要在 spawn
函数之前初始化 blacklisted_numbers
。当所有随机位置都在列表中时重置列表:
blacklisted_numbers = []
def spawn():
global blacklisted_numbers
random_locations = [(100, 440), (350, 440), (600, 440), (100, 260), (350, 260), (600, 260), (100, 80), (350, 80),
(600, 80)]
if len(blacklisted_numbers) == len(random_locations):
blacklisted_numbers = []
# [...]
或者您可以从 random_locations
中删除项目:
random_locations = []
def spawn():
global random_locations
if not random_locations:
random_locations = [(100, 440), (350, 440), (600, 440), (100, 260), (350, 260), (600, 260), (100, 80), (350, 80),
(600, 80)]
number = choice(random_locations)
random_locations.remove(number)
# [...]