0

I have included in my code a print statement saying what the following line is doing before a line of code is written. According to the console the error occurs. Don't see how this could have any effect, but I coded this on a linux machine and am trying to run it on windows. On a side note, in the line where I am trying to detect which rectangle was clicked. I cannot for the life of me get it to work. When I tell it to "print selected_rect[0]" I am expecting to see "ip_rect" (this is after i clicked the rectangle labeled "ip_rect" which is the top rectangle that appears), but it outputs "< rect(300, 148, 202, 54)>" (without the space between the first carrot and "rect". If I don't put that there, stack overflow registers is as code (rightfully so) and doesn't include it). So if someone could also guide me in the right direction with that, that would be awesome! Anyway here's the code with the included print statements.

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]

This is what is in the console after running the program.

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
  • The prints indicate that something is wrong with `pygame.font.SysFont`. I can't reproduce the error and also can't provide much help. Try to pass an existing font name instead of `None` to `SysFont`. And check out if `pygame.font.Font` works. – skrx Jul 09 '17 at 06:21
  • What pygame version do you use and how did you install it? – skrx Jul 09 '17 at 06:27

0 Answers0