我试图让它成为当蜘蛛触及主角(mc)时,pygame关闭。我不太了解pygame.Rect,但不应该这样做。这个工作?不知何故,pygame在添加此代码后仍会关闭:
mcrect = mc.get_rect()
spirect = spider_small.get_rect()
if mcrect.colliderect(spirect):
pygame.quit()
sys.exit()
整个代码供参考:
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)
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()
spirect = spider_small.get_rect()
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))
mc_x = 20
mc_y = 20
spider_x = 600
spider_y = 600
#Main loop
while True:
screen.blit(background,(0,0))
screen.blit(spider_small,(spider_x,spider_y))
screen.blit(mc,(mc_x,mc_y))
if mc_x > spider_x:
spider_x += .8
if mc_x < spider_x:
spider_x -= .8
if mc_y > spider_y:
spider_y += .8
if mc_y<spider_y:
spider_y -= .8
wait(1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if pygame.key.get_pressed()[K_LEFT]:
mc_x -= 3
elif pygame.key.get_pressed()[K_RIGHT]:
mc_x += 3
elif pygame.key.get_pressed()[K_UP]:
mc_y -= 3
elif pygame.key.get_pressed()[K_DOWN]:
mc_y += 3
pygame.display.update()
if mcrect.colliderect(spirect):
pygame.quit()
sys.exit()
else:
pass
答案 0 :(得分:0)
当您使用Rect
的{{1}}函数创建get_rect()
时,Surface
的{{1}}属性将始终为topleft
由于您永远不会更改Rect
和0, 0
,因此它们始终重叠,因为它们都具有相同的位置(mcrect
);因此spirect
始终为0, 0
。
您应该做的是摆脱mcrect.colliderect(spirect)
,True
,mc_x
和mc_y
变量并使用spider_x
和spider_y
跟踪图像的位置,例如而不是
mcrect
DO
spirect
等。等