![]() ![]() You learned how to create the game snake in Python along with concepts such as collision detection, image loading and event handling. Self._apple_surf = ( "block.jpg").convert() Self._image_surf = ( "block.jpg").convert() If x1 >= x2 and x1 = y2 and y1 <= y2 bsize: If x1 >= x2 and x1 = y2 and y1 self.updateCountMax: We create a new method to do that:ĭef isCollision( self,x1,y1,x2,y2,bsize): This simply means veryfing that the cordinate of the snake are intersecting with the coordinates of the apple. To know if the snakes position matches the apples position, we have to do collision detection. Self._apple_surf = ( "apple.png").convert() You can now move the block around the screen with the arrow keys. Self._display_surf.blit(self._image_surf,(,)) Self._image_surf = ( "pygame.png").convert() Self._display_surf = _mode((self.windowWidth,self.windowHeight), pygame.HWSURFACE) The complete code gives us the ability to move the player across the screen: from pygame. In Pygame we can get non-blocking keyboard input using this code: () In addition we define the actions a Player instance can do (movements): class Player:Ī player object can be created and variables can be modified using the movement methods. ![]() We define a class Player which holds the players position on the screen and the speed by which it moves. Getting started: Basic structure and event handling. ![]() This is very easy in the early phase of the game but is increasingly more difficult as the length of the snake grows. The goal of the game is to eat as many apples as possible without colliding into yourself. The player is represented as snake, which grows if it eats an apple. The game is an arcade game and it has very simple logic, which is why it is an ideal example to demonstrate how to build games with Pygame. Global variables are almost always a bad idea.In this tutorial you will learn how to build the game snake. You should stop using global variables and instead pass whatever variables you need between your functions. I think you should start looking at OOP pretty soon, but before that a glaring issue is that your functions all rely entirely on "side-effects" they don't accept any arguments and they return nothing. Usually people would write for _ in range(3): here to show that we don't actually care about the index of the iterator. Where the x in your for loop has nothing to do with the square dimensions. You also shouldn't use the same variable for different things, like this: def create_s(): Your function here: if (c.coords(s) directions>=600 or \Ĭ.coords(s) directions<=-30 or \Ĭ.coords(s) directions<=-30) and run: You should also take care in how you set up lines, and sometimes more cases are worth it simply since they make it easier to follow what's happening. Use clear and obvious names the common saying is that you would read a piece of code way more often than it is written, and it thus needs to be easy to understand-you might be surprised how little even your own code will make sense to you only days after having written it. Without deducing from functionality, I have no idea what variables like s and ap, or what functions like create_s() and s_move() do. Sp = c.create_text(10, 585, font='TimesNewRoman 10', text=f'speed: ', fill='white', anchor=W) If which_direction = -1: which_direction = 3 ![]() If which_direction = 4: which_direction = 0 Pause_text = c.create_text(302, 250, font='Terminal 30 bold', text="Press space to resume", fill='white') Global s, ap, run, which_direction, score If (c.coords(s) directions>=600 or \Ĭ.coords(s) directions>=600 or \Ĭ.coords(s) directions', restart) Square = c.create_rectangle(x, y, x 30, y 30, fill='', outline='white')Ĭ.move(s,directions,directions) Space - pause/start/restart game it dependsĬ = Canvas(root,bg='black', width=604, height=604, highlightthickness=0)Ĭ.create_text(302, 250, font='Terminal 30 bold', text="Press space to start", fill='white')ĭirections = ,] I know that I should use comments, I am sorry. I created snake as my first program with GUI, I didn't learn OOP yet, ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |