0

I am reading data from a file. Basically, those are coordinates where I want my ball to appear after every iteration. The code is working fine except for the fact that the output window 'Trial 1' crashes as soon as I press the exit button. This problem wasn't there before I added for t in range (np.size(T)):; however I require that. Please suggest some possible changes in the code to get rid of the problem.

import numpy as np
import pygame

pygame.init()

T = np.loadtxt('xy_shm1.txt', usecols=range(0,1))
Xcor = np.loadtxt('xy_shm1.txt', usecols=range(1,2))
Ycor = np.loadtxt('xy_shm1.txt', usecols=range(2,3))

clock = pygame.time.Clock()

background_colour = (255,255,255)
(width, height) = (800, 800)

class Particle():
    def __init__(self, xy, size):
        self.x, self.y = xy
        self.size = size
        self.colour = (0, 0, 255)
        self.thickness = 1


    def display(self):
        pygame.draw.circle(screen, self.colour, (int(self.x), int(self.y)), self.size, self.thickness)


    def move(self):

        self.x = Xcor[t] + 400
        self.y = Ycor[t] + 400

screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Trial 1')

number_of_particles = 1
my_particles = []

for n in range(number_of_particles):
    size = 5
    x = Xcor[0] + 400
    y = Ycor[0] + 400
    particle = Particle((x, y), size)

    my_particles.append(particle)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False



    for t in range(np.size(T)):

        screen.fill(background_colour)

        for particle in my_particles:

            particle.move()
            particle.display()

        pygame.display.flip()
        clock.tick(60)



pygame.quit()

1 Answers1

0

The main problem is that you are trying to draw multiple frames within a frame. The frame loop should look like this:

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Draw one frame here

    clock.tick(60) # If the game runs faster than 60fps, wait here

Note that in each iteration of the while loop, only one frame is drawn. In your current code however, you start the loop, check the events once, and then you draw a frame for each item in your list, without checking the events again.

This most likely causes the QUIT event to be missed, and the operating system intervening because the game seemingly is not responding.

In general, your code is quite messy. I suggest that you read some tutorials on pygame, or you will run into all sorts of similar problems. See for example: http://programarcadegames.com/python_examples/f.php?file=bouncing_rectangle.py

Meyer
  • 1,662
  • 7
  • 21
  • 20
  • This isn't working. There are 2 issues with this indentation. 1. The tick isn't working 2. The initial issue is still there. – Diptanil Roy Nov 24 '16 at 12:16
  • As I looked closer at your code, I found more and more issues. I have edited my answer to address the underlying problem of your code. – Meyer Nov 24 '16 at 12:53