Python - pygame instance entry

This article is a study notes. Adding their own understanding.

Ultimately is a moving ball

pygame: making games is a module, you can call in Python.

impor pygame # Import module pygame 
impor sys # sys import module
pygame.init () # initialize pygame
Screen with pygame.display.set_mode = ((600,400)) # setting window
while True: # main loop
    for event in pygame.event.get ( ): # Add the event to check
      if event.type == pygame.QUIT: # exit, exit the program when the mouse to click the exit button
        sys.exit ()
pygame.quit ()

The above code mainly realize: windows and exits.

pygame Impor  # Import module pygame 
Impor sys      # import sys module 
pygame.init ()  # initialize pygame 
Screen with pygame.display.set_mode = ((600,400))  # setting window 
ball = pygame.image.load ( "./ box.glf" ) # import pellets pictures, pictures of your own path to modify. 
ballrect = ball.get_rect () # can get pictures by get_rect () function position
print (ballrect) # here at the location of the image output by the look 
seppn = [1,1] position of the axis and y-axis #x
clock = pygame.time.Clock () # set the time, to be noted here that the Clock the first letter must be capitalized
the while True:  # main loop     clock.tick (100) # implementation of 100 times per second
    for event in pygame.event.get ():    # Add check the event       IF event.type == pygame.quit:    # exits, exit the program when the mouse to click the exit button         sys.exit ()
    ballrect = ballrect.move (seppn)
    iF ballrect.left <0 or ballrect.right> 600: # collision checking
      speed [0] = -speed [0 ] # negated
    IF ballrect.top <0 or ballrect.bottom> 400:
      Speed [. 1] = -speed [. 1] # negated
    screen. fill ((0,0,0)) # add a new background, because every time when we update the while loop generates a background, when the second time will generate a new context to cover small ball this time the ball looks like there will be no overlapping phenomenon
    screen.blit (ball, ballrect) # the ball and
ballrect displayed in the window
    pygam.display.flip () # display the contents of the window
pygame.quit()

 The code implementation: 1. Add pellets

        2. The display position of the ball

        3. Check the small ball collision

        4. Add a new background so that the ball does not seem to have overlapping phenomenon

        5. Set about the speed of the ball

        6. Complete the entire movement of the ball

The following code after the code is improved

pygame Impor  # Import module pygame 
Impor sys      # import sys module 
pygame.init ()  # initialize pygame 
size = width, height = 600,400 
Screen = with pygame.display.set_mode (size)  # setting window 
Ball pygame.image.load = ( " . /box.glf " )  # import pellets pictures, pictures of your own path to modify. 
= ball.get_rect ballrect ()  # by The get_rect () This function can acquire the position of the picture 
Print (ballrect)    # where output can look at the image location 
seppn = [1,1]  # X-axis and y-axis location 
Clock = pygame.time.Clock ()  # set the time to note here is that the first letter must be capitalized Clock 
color = (0,0,0)  # Set the color 
the while True:  # main loop 
    clock.tick (100)  # execute per 100 seconds 
    for Event in pygame.event.get ():    # Add check the event 
      IF event.type == pygame.quit:    # exit event, exit exit the program when the mouse button clicks 
        the sys.exit () 
    ballrect = ballrect.move (seppn)  # x-axis and y-axis 
    iF ballrect.left <0 or ballrect.right> width:    # collision check 
      speed [0] = -speed [0]    # negated 
    IF ballrect.top <0 or ballrect.bottom> height: 
      Speed [ . 1] = -speed [. 1]    # negated 
    screen. the Fill (Color)    # add a new background, because every time when we update the while loop generates a background, when the second time will generate a new background to cover the ball this time the ball would not seem overlapping phenomenon 
    screen.blit (ball, ballrect)  # the ball and ballrect displayed in the window 
    pygam.display.flip ()  # display the contents of the window 
pygame.quit ()
Improved version of the code

 

Guess you like

Origin www.cnblogs.com/jiekesi/p/11610714.html