我在代码中包含了一个print语句,说明在编写代码行之前,以下行是做什么的。根据控制台发生错误。不知道这会产生什么影响,但我在linux机器上编写了这个,并试图在Windows上运行它。在旁注中,在我试图检测单击哪个矩形的行中。我不能为我的生活得到它的工作。当我告诉" print selected_rect [0]"我期待看到" ip_rect" (这是在我点击标有" ip_rect"这是出现的顶部矩形)的矩形之后,但它输出"< rect(300,148,202,54)>" (没有第一个胡萝卜和" rect"之间的空格。如果我不把它放在那里,堆栈溢出寄存器就像代码一样(理所当然)并且不包含它)。因此,如果有人也可以用正确的方向指导我,那就太棒了!无论如何,这里的代码包含了打印声明。
print "importing pygame"
import pygame
print "importing base64"
import base64
print "initializing pygame"
pygame.init()
print "assigning screen size variables"
width = 800
height = 600
print "creating display"
face = pygame.display.set_mode((width, height))
print "setting caption"
pygame.display.set_caption("Messaging Interface")
print "creating first font"
the_font = pygame.font.SysFont(None, 25)
print "creating second font"
the_2_font = pygame.font.SysFont(None, 20)
print "creating first set of text"
target_ip_q_label = the_font.render("Who are you trying to message?", 1, (255, 255, 255))
print "creating second set of text"
message_q_label = the_font.render("What do you want to say?", 1, (255, 255, 255))
print "creating third set of text"
message_label = the_2_font.render("Target IP:", 1, (255, 255, 255))
print "blitting first text"
face.blit(target_ip_q_label, (275, 120))
print "blitting second text"
face.blit(message_q_label, (300, 320))
print "blitting third text"
face.blit(message_label, (310, 167.5))
print "creating first rectangle"
ip_rect = pygame.draw.rect(face, (255, 0, 0), (300, 150, 200, 50), 5)
print "creating second rectangle"
msg_rect = pygame.draw.rect(face, (255, 0, 0), (200, 350, 400, 100), 5)
print "creating list of rectangles"
rects = [ip_rect, msg_rect]
print "updating display"
pygame.display.update()
print "crating while loop"
while 1:
print "finding events"
for event in pygame.event.get():
print "checking if event is a keydown"
if event.type == pygame.KEYDOWN:
print "checking if it was the escape key"
if event.key == pygame.K_ESCAPE:
print 'quitting'
quit()
print "checking if the mouse was clicked"
if event.type == pygame.MOUSEBUTTONDOWN:
print "getting mouse position"
mouse_pos = pygame.mouse.get_pos()
print "detecting which rectangle the mouse clicked on"
selected_rect = [s for s in rects if s.rect.collidepoint(mouse_pos)]
print "printing which rectangle was clicked"
print selected_rect[0]
这是运行程序后控制台中的内容。
C:\Python27\python.exe D:/M.I/Interface.py
importing pygame
importing base64
initializing pygame
assigning screen size variables
creating display
setting caption
creating first font
Fatal Python error: (pygame parachute) Segmentation Fault
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Process finished with exit code 3