![]() Here we will not introduce the functions provided by the sprite module for the time being, because it is very simple to detect whether two balls collide, Just check whether the distance between the center of the ball is less than the sum of the radii of the two balls. The sprite module of pygame provides support for collision detection. Collision detection is a crucial problem that must be handled in most games. Usually, many objects appear in a game, and the "collision" between these objects is inevitable, such as shells hitting aircraft, boxes hitting the ground, etc. # Change the position of the ball every 50 milliseconds and refresh the window # Define variables to represent the position of the ball on the screen If you want to move the small ball in the above code, you can use variables to represent the position of the small ball, modify the position of the small ball in the loop, and then refresh the whole window. As long as a certain number of frames per second is reached, you can make a relatively smooth animation effect. In fact, to achieve animation effect, the principle itself is very simple, that is, play discontinuous pictures continuously. ![]() Speaking of animation This word is not unfamiliar to everyone. # Loads the image with the specified file nameīall_image = ('./res/ball.png') If you need to load the image directly into the window, you can use the function of the image module in pygame to load the image, and then render the image through the blit method of the previously obtained window object. # Refresh the current window (the rendering window renders the drawn image) # Draw a circle (parameters are: screen, color, center position, radius, 0 indicates filling circle) # Set the background color of the window (the color is a tuple composed of red, green and blue primary colors) The three colors are equivalent to a total of 24 bits, which is often referred to as "24 bit color representation". pygame uses color light to represent color Three primary colors Representation, that is, the RGB values of colors are specified through a tuple or list, and each value is between 0 and 255, because each primary color is represented by an 8-bit value. You can use the image browsing software to try to enlarge a picture several times, and you can see these points. The so-called pixel is a point on the screen. ![]() When indicating the position or setting the size, our default unit is pixel. It should be noted that the screen coordinate system sets the upper left corner of the screen as the coordinate origin (0, 0), the right is the positive direction of the x axis, and the down is the positive direction of the y axis. The graphics you can draw include lines, rectangles, polygons, circles, ellipses, arcs, etc. You can draw on the window through the function of the draw module in pygame. # Get events from the message queue and process them # Start an event loop to handle the events that occur # Initializes the window for display and sets the window size # Initialize the modules in the imported pygame The name of the game is "big ball eats small ball" 1. Pygame makes game developers no longer bound by the underlying language and can pay more attention to the functions and logic of the game. It is widely used in the development of games, simulators, players and so on. Pygame is built on SDL SDL is a cross platform multimedia development library, which is implemented in C language. Pygame is an open source Python module, which is specially used for the development of multimedia applications (such as video games), including support for images, sounds, videos, events, collisions, etc. 3, Snake game 1, Game development using Pygame
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |