我包括了这个:
if spirect.top > mcrect.top:
spirect.move_ip(-3,0)
if spirect.top < mcrect.top:
spirect.move_ip(3,0)
if spirect.left > mcrect.left:
spirect.move_ip(0,-3)
if spirect.left < mcrect.left:
spirect.move_ip(0,3)
在我的主循环中让我的游戏中的蜘蛛向主角移动。在尝试这个之后,无论如何,蜘蛛(spirect)只是向一个方向移动。我该如何解决? 所有相关代码:
import pygame
import sys
import time
import math
#Initializing stuff and setting display settings
wait = time.sleep
'''
print("What is your preferred screen width and height?(800,600 or greater.)")
wait(1)
print("If your chosen resolution is below 800,600 , you will be automatically set to 800,600")
wait(1)
preferred_screen_width = raw_input("Width?")
print("Dank MLG processing...")
wait(1.5)
preferred_screen_height = raw_input("Height?")
print("More dank MLG processing...")
wait(1.5)'''
#TEMP
preferred_screen_width = 800
preferred_screen_height = 600
pygame.init()
pygame.mixer.pre_init(44100, 16, 2, 4096)
from pygame.locals import *
'''Concepting
'''
sound = pygame.mixer.Sound('untitled.ogg')
sound.play(-1)
if int(preferred_screen_width) < 800:
print("Illegal choice. Setting to 800,600")
preferred_screen_width = 800
preferred_screen_height = 600
elif int(preferred_screen_height) < 600:
print("Illegal choice. Setting to 800,600")
preferred_screen_width = 800
preferred_screen_height = 600
else:
print("Starting game")
#Ingame
pygame.display.set_caption("Spider Avoiderer")
spider_small = pygame.image.load("sag.png")
mc = pygame.image.load("mc.png")
mcrect = mc.get_rect(top = 20,left = 20)
spirect = spider_small.get_rect(top = 20,left = 20)
really_red = 255,0,0
dark_red = 171,0,0
deep_blue = 0,0,141
really_blue = 0,0,255
aa = raw_input("Would you like to use a custom background?(yes or no)")
if aa.lower() == "yes":
print("Choose one of these colors(type exactly as written)")
aaa = raw_input("really red, dark red, deep blue, really blue, or custom RGB")
if aaa.lower() == "really red":
penis = really_red
elif aaa.lower() == "dark red":
penis = dark_red
elif aaa.lower() == "deep blue":
penis = deep_blue
elif aaa.lower() == "really blue":
penis = really_blue
elif aaa.lower() == "custom rgb":
print("Enter your RGB coloring:")
rgb_red = raw_input("Value of red? ")
rgb_green = raw_input("Value of green? ")
rgb_blue = raw_input("Value of blue?")
penis = int(rgb_red),int(rgb_green),int(rgb_blue)
elif aa.lower() == "no":
penis = 100,210,25
else:
penis = 100,210,25
screen = pygame.display.set_mode((int(preferred_screen_width),int(preferred_screen_height)))
#fps
clock = pygame.time.Clock()
clock.tick(30)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((penis))
#Main loop
while True:
screen.blit(background,(0,0))
screen.blit(spider_small,(spirect))
screen.blit(mc,(mcrect))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if pygame.key.get_pressed()[K_LEFT]:
mcrect.move_ip(-3,0)
elif pygame.key.get_pressed()[K_RIGHT]:
mcrect.move_ip(3,0)
elif pygame.key.get_pressed()[K_UP]:
mcrect.move_ip(0,-3)
elif pygame.key.get_pressed()[K_DOWN]:
mcrect.move_ip(0,3)
if spirect.top > mcrect.top:
spirect.move_ip(3,0)
if spirect.top < mcrect.top:
spirect.move_ip(-3,0)
if spirect.left > mcrect.left:
spirect.move_ip(0,3)
if spirect.left < mcrect.left:
spirect.move_ip(0,-3)
pygame.display.update()
答案 0 :(得分:0)
你比较了蜘蛛和mc的top
(y
坐标),但是你改变了x
坐标:)所以蜘蛛向错误的方向移动而且它不能停止因为它的top
仍然不同于mc
top:)
if spirect.top > mcrect.top:
spirect.move_ip(0,-3) # before: (3,0)
elif spirect.top < mcrect.top:
spirect.move_ip(0,3) # before: (-3,0)
if spirect.left > mcrect.left:
spirect.move_ip(-3,0) # before: (0, 3)
elif spirect.left < mcrect.left:
spirect.move_ip(3,0) # before: (0,-3)
但现在spider
的移动速度与mc
一样快,因此它们始终位于同一位置。更快mc
或将spider
放在远离mc
mc_speed = 6 # before: 3
if pygame.key.get_pressed()[K_LEFT]:
mcrect.move_ip(-mc_speed,0)
elif pygame.key.get_pressed()[K_RIGHT]:
mcrect.move_ip(mc_speed,0)
# use `if` instead of `elif`
if pygame.key.get_pressed()[K_UP]:
mcrect.move_ip(0,-mc_speed)
elif pygame.key.get_pressed()[K_DOWN]:
mcrect.move_ip(0,mc_speed)