I am trying to make a game like pong using pygame I currently have the two rectangles however one glitches I dont know why, I commented win.fill((0, 0, 0))
and it stops one of the rectangles from glitching but the other one does not this is what I have:
# import pygame module in this program
import pygame
# activate the pygame library .
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
# create the display surface object
# of specific dimension..e(500, 500).
win = pygame.display.set_mode((1280, 720))
# set the pygame window name
pygame.display.set_caption("Pong")
# object1 current co-ordinates
x = 15
y = 280
# object2 current co-ordinates
x1 = 1245
y1 = 280
# dimensions of the object1
width = 20
height = 130
# dimensions of the object2
width1 = 20
height1 = 130
# velocity / speed of movement
vel = 10
# Indicates pygame is running
run = True
# infinite loop
while run:
# creates time delay of 10ms
pygame.time.delay(0)
# iterate over the list of Event objects
# that was returned by pygame.event.get() method.
for event in pygame.event.get():
# if event object type is QUIT
# then quitting the pygame
# and program both.
if event.type == pygame.QUIT:
# it will make exit the while loop
run = False
# stores keys pressed
keys = pygame.key.get_pressed()
# if left arrow key is pressed
if keys[pygame.K_w] and y > 0:
# decrement in y co-ordinate
y -= vel
# if left arrow key is pressed
if keys[pygame.K_s] and y < 720-height:
# increment in y co-ordinate
y = vel
# completely fill the surface object
# with black colour
win.fill((0, 0, 0))
# drawing object on screen which is rectangle here
pygame.draw.rect(win, (0, 0, 255), (x, y, width, height))
# it refreshes the window
pygame.display.update()
keys2 = pygame.key.get_pressed()
# if left arrow key is pressed
if keys2[pygame.K_UP] and y1 > 0:
# decrement in y co-ordinate
y1 -= vel
# if left arrow key is pressed
if keys2[pygame.K_DOWN] and y1 < 720-height:
# increment in y co-ordinate
y1 = vel
# completely fill the surface object
# with black colour
win.fill((0, 0, 0))
# drawing object on screen which is rectangle here
pygame.draw.rect(win, (255, 255, 255), (x1, y1, width1, height1))
# it refreshes the window
pygame.display.update()
# closes the pygame window
pygame.quit()
CodePudding user response:
Call pygame.disaply.update()
only once at the end of the application loop. Multiple calls to pygame.display.update()
or pygame.display.flip()
cause flickering. See Why is the PyGame animation is flickering. Also clear the display only once per frame.
You are actually drawing on a Surface
object. If you draw on the Surface associated to the PyGame display, this is not immediately visible in the display. The changes become visible, when the display is updated with either pygame.display.update()
or pygame.display.flip()
.
If you clear the display, all previously drawn elements become invisible. Therefore, clear the display once at the beginning of the frame and update it once at the end of the frame.
The typical PyGame application loop has to:
- limit the frames per second to limit CPU usage with
pygame.time.Clock.tick
- handle the events by calling either
pygame.event.pump()
orpygame.event.get()
. - update the game states and positions of objects dependent on the input events and time (respectively frames)
- clear the entire display or draw the background
- draw the entire scene (
blit
all the objects) - update the display by calling either
pygame.display.update()
orpygame.display.flip()
clock = pygame.time.Clock()
run = True
# application loop
while run:
# limit the frames per second to limit CPU usage
clock.tick(100)
# handle the events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# update the game states and positions of objects
keys = pygame.key.get_pressed()
if keys[pygame.K_w] and y > 0:
y -= vel
if keys[pygame.K_s] and y < 720-height:
y = vel
if keys[pygame.K_UP] and y1 > 0:
y1 -= vel
if keys[pygame.K_DOWN] and y1 < 720-height:
y1 = vel
# clear the display
win.fill((0, 0, 0))
# draw the entire scene
pygame.draw.rect(win, (0, 0, 255), (x, y, width, height))
pygame.draw.rect(win, (255, 255, 255), (x1, y1, width1, height1))
# update the display
pygame.display.update()