Pygame 使用 colliderect 时关闭
Pygame closing when using colliderect
我正在努力做到当主角(mc)被蜘蛛触动时,pygame closes.I对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
当您使用 Surface
的 get_rect()
功能创建 Rect
时,Rect
的 topleft
属性 将始终成为 0, 0
.
由于您永远不会更改 mcrect
和 spirect
,因此它们总是重叠,因为它们的位置相同 (0, 0
);所以 mcrect.colliderect(spirect)
将永远是 True
.
你应该做的是摆脱 mc_x
、mc_y
、spider_x
和 spider_y
变量并使用 mcrect
和 spirect
跟踪图像的位置,例如而不是
mcrect = mc.get_rect()
...
screen.blit(mc,(mc_x,mc_y))
...
mc_y += 3
做
mcrect = mc.get_rect(top=20, left=20)
...
screen.blit(mc, mcrect)
...
mcrect.move_ip(0, 3)
等等等
我正在努力做到当主角(mc)被蜘蛛触动时,pygame closes.I对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
当您使用 Surface
的 get_rect()
功能创建 Rect
时,Rect
的 topleft
属性 将始终成为 0, 0
.
由于您永远不会更改 mcrect
和 spirect
,因此它们总是重叠,因为它们的位置相同 (0, 0
);所以 mcrect.colliderect(spirect)
将永远是 True
.
你应该做的是摆脱 mc_x
、mc_y
、spider_x
和 spider_y
变量并使用 mcrect
和 spirect
跟踪图像的位置,例如而不是
mcrect = mc.get_rect()
...
screen.blit(mc,(mc_x,mc_y))
...
mc_y += 3
做
mcrect = mc.get_rect(top=20, left=20)
...
screen.blit(mc, mcrect)
...
mcrect.move_ip(0, 3)
等等等