Nov-16-2018, 12:21 PM
I wrote some codes for my ball to don't spawn border over.But my ball spawns over of border.Anyone you help me this logic problem? If you won't change my coding logic , I will happy.Also,I am sorry for my english.Now I'm sharing my code.
import pygame import random import time pygame.init() display_width = 600 display_height = 400 gameDisplay = pygame.display.set_mode((display_width,display_height)) white = (255,255,255) red = (255,0,0) ball_x = int(display_width/2) ball_y = int(display_height/2) clock = pygame.time.Clock() gameExit = False while not gameExit: ball_list = [] for e in pygame.event.get(): if e.type == pygame.QUIT: gameExit = True ball_speed_x = random.randrange(-5,5) ball_speed_y = random.randrange(-5,5) while ball_speed_x == 0 and ball_speed_y == 0: print(1) ball_speed_x = random.randrange(-5,5) ball_speed_y = random.randrange(-5,5) if ball_x > display_width - 10 or ball_x < 10: ball_speed_x *= -1 if ball_y > display_height - 10 or ball_y < 10: ball_speed_y *= -1 ball_x += ball_speed_x ball_y += ball_speed_y ball_list.append([ball_x,ball_y]) gameDisplay.fill(white) for ball_coordinates in ball_list: pygame.draw.circle(gameDisplay,red,[ball_coordinates[0],ball_coordinates[1]],10,10) # print("Coordinates:" + str(ball_coordinates) + "\n" + "Ball x:" + str(ball_x) + "," + "Ball y:" + str(ball_y) + "\n" + "ball speed x:" +str(ball_speed_x) + "ball speed y:"+ str(ball_speed_y)) pygame.display.flip() clock.tick(15) pygame.quit() quit()