<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ilya</title>
    <description>The latest articles on DEV Community by Ilya (@ilya_romanov).</description>
    <link>https://dev.to/ilya_romanov</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F746468%2Fcb3d0584-89f1-42e0-9456-f8ccf398a124.jpg</url>
      <title>DEV Community: Ilya</title>
      <link>https://dev.to/ilya_romanov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ilya_romanov"/>
    <language>en</language>
    <item>
      <title>Tic-tac-toe in Python</title>
      <dc:creator>Ilya</dc:creator>
      <pubDate>Mon, 08 Nov 2021 17:23:31 +0000</pubDate>
      <link>https://dev.to/ilya_romanov/tic-tac-toe-in-python-215m</link>
      <guid>https://dev.to/ilya_romanov/tic-tac-toe-in-python-215m</guid>
      <description>&lt;p&gt;Today we are faced with the task of writing a game of tic-tac-toe on python. Recall that tic-tac-toe is a logical game for two players on a 3x3 square field.&lt;/p&gt;

&lt;p&gt;To begin with, we will set the field. Our field will be a one-dimensional list with numbers from 1 to 9. To create it, we will use the &lt;strong&gt;range()&lt;/strong&gt; function&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;board = range(1,10)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Now let's write a function that will output our field in the usual format&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def draw_board(board):&lt;br&gt;
    print "-------------"&lt;br&gt;
    for i in range(3):&lt;br&gt;
        print "|", board[0+i*3], "|", board[1+i*3], "|", board[2+i*3], "|"&lt;br&gt;
        print "-------------"&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;It's time to allow users to enter data into our game. Writing the &lt;strong&gt;take_input&lt;/strong&gt; function&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ef take_input(player_token):&lt;br&gt;
    valid = False&lt;br&gt;
    while not valid:&lt;br&gt;
        player_answer = raw_input("Where do we put " + player_token+"? ")&lt;br&gt;
        try:&lt;br&gt;
            player_answer = int(player_answer)&lt;br&gt;
except:&lt;br&gt;
print "Incorrect input. Are you sure you entered a number?"&lt;br&gt;
continue&lt;br&gt;
        if player_answer &amp;gt;= 1 and player_answer &amp;lt;= 9:&lt;br&gt;
            if (str(board[player_answer-1]) not in "XO"):&lt;br&gt;
                board[player_answer-1] = player_token&lt;br&gt;
                valid = True&lt;br&gt;
            else:&lt;br&gt;
print "This cell is already occupied"&lt;br&gt;
        else:&lt;br&gt;
            print "Incorrect input. Enter a number from 1 to 9 to be like."&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;As you can see, the &lt;strong&gt;take_input&lt;/strong&gt; function takes the &lt;strong&gt;player_token&lt;/strong&gt; parameter - a cross or a toe, depending on whose turn it is now. We need to limit the user's choice to numbers from 1 to 9. To do this, we use the try/except and if/else constructs to make sure that the selected cell is not occupied. Note that the take_input function does not return any value, but only modifies the existing board list.&lt;/p&gt;

&lt;p&gt;It remains to write a function for checking the playing field. Let's call this function &lt;strong&gt;check_win&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def check_win(board):&lt;br&gt;
    win_coord = ((0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6))&lt;br&gt;
    for each in win_coord:&lt;br&gt;
        if board[each[0]] == board[each[1]] == board[each[2]]:&lt;br&gt;
            return board[each[0]]&lt;br&gt;
    return False&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Checking the results of the tic-tac-toe game is a fairly common programming task. As often happens, the same task in programming can be solved in several ways. In this case, we simply created a tuple with the winning coordinates and ran a for loop through it. If the symbols in all three specified cells are equal, we return the winning symbol, otherwise, we return the value False. At the same time, it is important to remember that not an empty string (our winning symbol) when converting it to a logical type, it will return True (we will need this in the future).&lt;/p&gt;

&lt;p&gt;It remains to create the &lt;strong&gt;main&lt;/strong&gt; function, in which we will put together all the described functions.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def main(board):&lt;br&gt;
    counter = 0&lt;br&gt;
    win = False&lt;br&gt;
    while not win:&lt;br&gt;
        draw_board(board)&lt;br&gt;
        if counter % 2 == 0:&lt;br&gt;
            take_input("X")&lt;br&gt;
        else:&lt;br&gt;
            take_input("O")&lt;br&gt;
        counter += 1&lt;br&gt;
        if counter &amp;gt; 4:&lt;br&gt;
            tmp = check_win(board)&lt;br&gt;
            if tmp:&lt;br&gt;
                print tmp, "won!"&lt;br&gt;
                win = True&lt;br&gt;
                break&lt;br&gt;
        if counter == 9:&lt;br&gt;
            print "drawn game!"&lt;br&gt;
            break&lt;br&gt;
    draw_board(board)def main(board):&lt;br&gt;
    counter = 0&lt;br&gt;
    win = False&lt;br&gt;
    while not win:&lt;br&gt;
        draw_board(board)&lt;br&gt;
        if counter % 2 == 0:&lt;br&gt;
            take_input("X")&lt;br&gt;
        else:&lt;br&gt;
            take_input("O")&lt;br&gt;
        counter += 1&lt;br&gt;
        if counter &amp;gt; 4:&lt;br&gt;
            tmp = check_win(board)&lt;br&gt;
            if tmp:&lt;br&gt;
                print tmp, "won!"&lt;br&gt;
                win = True&lt;br&gt;
                break&lt;br&gt;
        if counter == 9:&lt;br&gt;
            print "drawn game!"&lt;br&gt;
            break&lt;br&gt;
    draw_board(board)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;The operation of the main function is extremely clear, except that lines 45 and 46 may cause misunderstanding. We are waiting for the counter variable to become greater than 4 to avoid a deliberately unnecessary call to the check_win function (no one can win for sure until the fifth movie). The tmp variable was created again in order not to call the check_in function once again, we just "remember" its value and, if necessary, use it on line 48. The benefits of this approach are not so noticeable when working with small amounts of data, but in general, such savings in processor time are good practice.&lt;/p&gt;

&lt;p&gt;Now we can safely play by running the main(board)&lt;/p&gt;

&lt;p&gt;Have a good game!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>python</category>
      <category>tutorial</category>
      <category>gamedev</category>
    </item>
    <item>
      <title>the Snake game in Python</title>
      <dc:creator>Ilya</dc:creator>
      <pubDate>Sat, 06 Nov 2021 20:50:06 +0000</pubDate>
      <link>https://dev.to/ilya_romanov/the-snake-game-in-python-1enf</link>
      <guid>https://dev.to/ilya_romanov/the-snake-game-in-python-1enf</guid>
      <description>&lt;h1&gt;
  
  
  1. Installing Pygame
&lt;/h1&gt;

&lt;p&gt;The first thing we need to do is install the Pygame library. This can be done by simply running the following command:&lt;/p&gt;

&lt;p&gt;pip install pygame&lt;/p&gt;

&lt;p&gt;After doing this, just import this library and start developing the game. But before that, let's take a look at the main functions of this library, which we will use when creating a game.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Function&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;init()&lt;/td&gt;
&lt;td&gt;Initializes all Pygame modules (returns a tuple in case of success or failure).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;display.set_mode()&lt;/td&gt;
&lt;td&gt;To create a surface, it takes either a list or a tuple as a parameter (a tuple is preferable).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;update()&lt;/td&gt;
&lt;td&gt;Refreshes the screen.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;quit()&lt;/td&gt;
&lt;td&gt;Used to initialize all modules.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;set_caption()&lt;/td&gt;
&lt;td&gt;Sets the title text at the top of the screen&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;event.get()&lt;/td&gt;
&lt;td&gt;Returns a list of all events.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Surface.fill()&lt;/td&gt;
&lt;td&gt;Fills the space with a solid colour.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;time.Clock()&lt;/td&gt;
&lt;td&gt;Time tracking&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;font.SysFont()&lt;/td&gt;
&lt;td&gt;Sets the Pygame font using system resources.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h1&gt;
  
  
  2. Creating a screen
&lt;/h1&gt;

&lt;p&gt;To create a screen using Pygame, you need to use the &lt;strong&gt;display.set_mode()&lt;/strong&gt; function. You also need to use the &lt;strong&gt;init()&lt;/strong&gt; method to initialize the screen at the beginning of the code and the &lt;strong&gt;quit()&lt;/strong&gt; method to close it at the end. The &lt;strong&gt;update()&lt;/strong&gt; method is used to apply any changes to the screen. There is also a &lt;strong&gt;flip()&lt;/strong&gt; method that works in a similar way to &lt;strong&gt;update()&lt;/strong&gt;. The only difference is that the &lt;strong&gt;flip()&lt;/strong&gt; method rewrites the entire screen, and the &lt;strong&gt;update()&lt;/strong&gt; method applies exactly the changes (although if it is used without parameters, it also rewrites the entire screen).&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import pygame&lt;br&gt;
pygame.init()&lt;br&gt;
dis=pygame.display.set_mode((400,300))&lt;br&gt;
pygame.display.update()&lt;br&gt;
pygame.quit()&lt;br&gt;
quit()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;However, when you run this code, the screen will appear only for a moment, and then disappear. To fix this error, we will use the &lt;strong&gt;while&lt;/strong&gt; loop, which will run until the end of the game:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import pygame&lt;br&gt;
pygame.init()&lt;br&gt;
dis=pygame.display.set_mode((400,300))&lt;br&gt;
pygame.display.update()&lt;br&gt;
pygame.display.set_caption('Snake game by Pythonist')&lt;br&gt;
game_over=False&lt;br&gt;
while not game_over:&lt;br&gt;
    for event in pygame.event.get():&lt;br&gt;
        print(event)&lt;br&gt;
 pygame.quit()&lt;br&gt;
quit()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Now, by running this code, you will see that the screen does not disappear as before. It will display all the actions of the game. We achieved this thanks to the &lt;strong&gt;event.get()&lt;/strong&gt; function. Also, using the &lt;strong&gt;display.set_caption()&lt;/strong&gt; function, we displayed the title of our screen — 'Snake game by Pythonist’.&lt;br&gt;
Now we have a screen for the game, but when you click on the close button, the screen will not close. This is because we have not provided for such behaviour. To solve this problem, Pygame provides a "QIUT" event, which we use as follows:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import pygame&lt;br&gt;
pygame.init()&lt;br&gt;
dis=pygame.display.set_mode((400,300))&lt;br&gt;
pygame.display.update()&lt;br&gt;
pygame.display.set_caption('Snake game by Edureka')&lt;br&gt;
game_over=False&lt;br&gt;
while not game_over:&lt;br&gt;
    for event in pygame.event.get():&lt;br&gt;
        if event.type==pygame.QUIT:&lt;br&gt;
            game_over=True&lt;br&gt;
 pygame.quit()&lt;br&gt;
quit()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Now that our screen is fully prepared, we have to draw a snake on it. The following section is devoted to this.&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Creating a snake
&lt;/h1&gt;

&lt;p&gt;Before creating a snake, we initiate several colour variables to colourize the snake itself, the food and the screen. Pygame uses the RGB colour scheme (RED, GREEN, BLUE). Setting all colors to &lt;strong&gt;0&lt;/strong&gt; corresponds to black, and &lt;strong&gt;255&lt;/strong&gt; corresponds to white, respectively.&lt;br&gt;
In fact, our snake is a rectangle. To draw a rectangle in Pygame, you can use the &lt;strong&gt;draw.rect()&lt;/strong&gt; function, which will draw us a rectangle of a given color and size.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import pygame&lt;br&gt;
pygame.init()&lt;br&gt;
dis=pygame.display.set_mode((400,300)) &lt;br&gt;
pygame.display.set_caption('Snake game by Pythonist')&lt;br&gt;
blue=(0,0,255)&lt;br&gt;
red=(255,0,0)&lt;br&gt;
game_over=False&lt;br&gt;
while not game_over:&lt;br&gt;
    for event in pygame.event.get():&lt;br&gt;
        if event.type==pygame.QUIT:&lt;br&gt;
            game_over=True&lt;br&gt;
    pygame.draw.rect(dis,blue,[200,150,10,10])&lt;br&gt;
    pygame.display.update()&lt;br&gt;
pygame.quit()&lt;br&gt;
quit()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;As you can see, the snake is created in the form of a blue rectangle. Now we need to teach her to move.&lt;/p&gt;

&lt;h1&gt;
  
  
  4. Snake Movement
&lt;/h1&gt;

&lt;p&gt;To move the snake, we will use key events from the &lt;strong&gt;KEYDOWN&lt;/strong&gt; class of the Pygame library. The &lt;strong&gt;K_UP&lt;/strong&gt;, &lt;strong&gt;K_DOWN&lt;/strong&gt;, &lt;strong&gt;K_LEFT&lt;/strong&gt;, and &lt;strong&gt;K_RIGHT&lt;/strong&gt; events will cause the snake to move up, down, left, and right, respectively. Also, the display colour changes from black (by default) to white using the &lt;strong&gt;fill()&lt;/strong&gt; method.&lt;br&gt;
To save the &lt;strong&gt;x&lt;/strong&gt; and &lt;strong&gt;y&lt;/strong&gt; coordinate changes, we have created two new variables: &lt;strong&gt;x1_change&lt;/strong&gt; and &lt;strong&gt;y1_change&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import pygame&lt;br&gt;
pygame.init()&lt;br&gt;
white = (255, 255, 255)&lt;br&gt;
black = (0, 0, 0)&lt;br&gt;
red = (255, 0, 0)&lt;br&gt;
dis = pygame.display.set_mode((800, 600))&lt;br&gt;
pygame.display.set_caption('Snake game by Pythonist')&lt;br&gt;
game_over = False&lt;br&gt;
x1 = 300&lt;br&gt;
y1 = 300&lt;br&gt;
x1_change = 0       &lt;br&gt;
y1_change = 0&lt;br&gt;
clock = pygame.time.Clock()&lt;br&gt;
while not game_over:&lt;br&gt;
    for event in pygame.event.get():&lt;br&gt;
        if event.type == pygame.QUIT:&lt;br&gt;
            game_over = True&lt;br&gt;
        if event.type == pygame.KEYDOWN:&lt;br&gt;
            if event.key == pygame.K_LEFT:&lt;br&gt;
                x1_change = -10&lt;br&gt;
                y1_change = 0&lt;br&gt;
            elif event.key == pygame.K_RIGHT:&lt;br&gt;
                x1_change = 10&lt;br&gt;
                y1_change = 0&lt;br&gt;
            elif event.key == pygame.K_UP:&lt;br&gt;
                y1_change = -10&lt;br&gt;
                x1_change = 0&lt;br&gt;
            elif event.key == pygame.K_DOWN:&lt;br&gt;
                y1_change = 10&lt;br&gt;
                x1_change = 0&lt;br&gt;
    x1 += x1_change&lt;br&gt;
    y1 += y1_change&lt;br&gt;
    dis.fill(white)&lt;br&gt;
    pygame.draw.rect(dis, black, [x1, y1, 10, 10])&lt;br&gt;
    pygame.display.update()&lt;br&gt;
 clock.tick(30)&lt;br&gt;
pygame.quit()&lt;br&gt;
quit()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  5. "Game over" when the snake reaches the border.
&lt;/h1&gt;

&lt;p&gt;In the snake game, the player loses if he touches the border of the screen. To set this behaviour, we must use the &lt;strong&gt;if&lt;/strong&gt; statement, which will make sure that the x and y coordinates are smaller than the screen size. We will use variables for this, so that you can then, on occasion, easily make any changes to the game.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import pygame&lt;br&gt;
import time&lt;br&gt;
pygame.init()&lt;br&gt;
white = (255, 255, 255)&lt;br&gt;
black = (0, 0, 0)&lt;br&gt;
red = (255, 0, 0)&lt;br&gt;
dis_width = 800&lt;br&gt;
dis_height  = 600&lt;br&gt;
dis = pygame.display.set_mode((dis_width, dis_width))&lt;br&gt;
pygame.display.set_caption('Snake game by Pythonist')&lt;br&gt;
game_over = False&lt;br&gt;
x1 = dis_width/2&lt;br&gt;
y1 = dis_height/2&lt;br&gt;
snake_block=10&lt;br&gt;
x1_change = 0&lt;br&gt;
y1_change = 0&lt;br&gt;
clock = pygame.time.Clock()&lt;br&gt;
snake_speed=30&lt;br&gt;
font_style = pygame.font.SysFont(None, 50)&lt;br&gt;
def message(msg,color):&lt;br&gt;
    mesg = font_style.render(msg, True, color)&lt;br&gt;
    dis.blit(mesg, [dis_width/2, dis_height/2])&lt;br&gt;
while not game_over:&lt;br&gt;
    for event in pygame.event.get():&lt;br&gt;
        if event.type == pygame.QUIT:&lt;br&gt;
            game_over = True&lt;br&gt;
        if event.type == pygame.KEYDOWN:&lt;br&gt;
            if event.key == pygame.K_LEFT:&lt;br&gt;
                x1_change = -snake_block&lt;br&gt;
                y1_change = 0&lt;br&gt;
            elif event.key == pygame.K_RIGHT:&lt;br&gt;
                x1_change = snake_block&lt;br&gt;
                y1_change = 0&lt;br&gt;
            elif event.key == pygame.K_UP:&lt;br&gt;
                y1_change = -snake_block&lt;br&gt;
                x1_change = 0&lt;br&gt;
            elif event.key == pygame.K_DOWN:&lt;br&gt;
                y1_change = snake_block&lt;br&gt;
                x1_change = 0&lt;br&gt;
    if x1 &amp;gt;= dis_width or x1 &amp;lt; 0 or y1 &amp;gt;= dis_height or y1 &amp;lt; 0:&lt;br&gt;
        game_over = True&lt;br&gt;
    x1 += x1_change&lt;br&gt;
    y1 += y1_change&lt;br&gt;
    dis.fill(white)&lt;br&gt;
    pygame.draw.rect(dis, black, [x1, y1, snake_block, snake_block])&lt;br&gt;
    pygame.display.update()&lt;br&gt;
    clock.tick(snake_speed)&lt;br&gt;
message("You lost",red)&lt;br&gt;
pygame.display.update()&lt;br&gt;
time.sleep(2)&lt;br&gt;
pygame.quit()&lt;br&gt;
quit()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  6. Adding Food
&lt;/h1&gt;

&lt;p&gt;Now we will add some food for the snake, and when it crosses it, we will display the message "Yummy!! In addition, we will make small changes that will allow the player to stop the game, as well as start it again in case of defeat.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import pygame&lt;br&gt;
import time&lt;br&gt;
import random&lt;br&gt;
pygame.init()&lt;br&gt;
white = (255, 255, 255)&lt;br&gt;
black = (0, 0, 0)&lt;br&gt;
red = (255, 0, 0)&lt;br&gt;
blue = (0, 0, 255)&lt;br&gt;
dis_width = 800&lt;br&gt;
dis_height = 600&lt;br&gt;
dis = pygame.display.set_mode((dis_width, dis_height))&lt;br&gt;
pygame.display.set_caption('Snake Game by Edureka')&lt;br&gt;
clock = pygame.time.Clock()&lt;br&gt;
snake_block = 10&lt;br&gt;
snake_speed = 30&lt;br&gt;
font_style = pygame.font.SysFont(None, 30)&lt;br&gt;
def message(msg, color):&lt;br&gt;
    mesg = font_style.render(msg, True, color)&lt;br&gt;
    dis.blit(mesg, [dis_width/3, dis_height/3])&lt;br&gt;
def gameLoop():  # creating a function&lt;br&gt;
    game_over = False&lt;br&gt;
    game_close = False&lt;br&gt;
    x1 = dis_width / 2&lt;br&gt;
    y1 = dis_height / 2&lt;br&gt;
    x1_change = 0&lt;br&gt;
    y1_change = 0&lt;br&gt;
    foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0&lt;br&gt;
    foody = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0&lt;br&gt;
    while not game_over:&lt;br&gt;
        while game_close == True:&lt;br&gt;
            dis.fill(white)&lt;br&gt;
            message("You Lost! Press Q-Quit or C-Play Again", red)&lt;br&gt;
            pygame.display.update()&lt;br&gt;
            for event in pygame.event.get():&lt;br&gt;
                if event.type == pygame.KEYDOWN:&lt;br&gt;
                    if event.key == pygame.K_q:&lt;br&gt;
                        game_over = True&lt;br&gt;
                        game_close = False&lt;br&gt;
                    if event.key == pygame.K_c:&lt;br&gt;
                        gameLoop()&lt;br&gt;
        for event in pygame.event.get():&lt;br&gt;
            if event.type == pygame.QUIT:&lt;br&gt;
                game_over = True&lt;br&gt;
            if event.type == pygame.KEYDOWN:&lt;br&gt;
                if event.key == pygame.K_LEFT:&lt;br&gt;
                    x1_change = -snake_block&lt;br&gt;
                    y1_change = 0&lt;br&gt;
                elif event.key == pygame.K_RIGHT:&lt;br&gt;
                    x1_change = snake_block&lt;br&gt;
                    y1_change = 0&lt;br&gt;
                elif event.key == pygame.K_UP:&lt;br&gt;
                    y1_change = -snake_block&lt;br&gt;
                    x1_change = 0&lt;br&gt;
                elif event.key == pygame.K_DOWN:&lt;br&gt;
                    y1_change = snake_block&lt;br&gt;
                    x1_change = 0&lt;br&gt;
        if x1 &amp;gt;= dis_width or x1 &amp;lt; 0 or y1 &amp;gt;= dis_height or y1 &amp;lt; 0:&lt;br&gt;
            game_close = True&lt;br&gt;
        x1 += x1_change&lt;br&gt;
        y1 += y1_change&lt;br&gt;
        dis.fill(white)&lt;br&gt;
        pygame.draw.rect(dis, blue, [foodx, foody, snake_block, snake_block])&lt;br&gt;
        pygame.draw.rect(dis, black, [x1, y1, snake_block, snake_block])&lt;br&gt;
        pygame.display.update()&lt;br&gt;
        if x1 == foodx and y1 == foody:&lt;br&gt;
            print("Yummy!!")&lt;br&gt;
        clock.tick(snake_speed)&lt;br&gt;
    pygame.quit()&lt;br&gt;
    quit()&lt;br&gt;
gameLoop()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  7. Increasing the length of the snake
&lt;/h1&gt;

&lt;p&gt;The following code will increase the length of the snake after it has absorbed food. Also, if the snake collides with its tail, the game ends and the message is displayed“ "You Lost! Press Q-Quit or C-Play Again“. The length of the snake is stored in the list, and the base values are set in the following code.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```import pygame&lt;br&gt;
import time&lt;br&gt;
import random&lt;br&gt;
pygame.init()&lt;br&gt;
white = (255, 255, 255)&lt;br&gt;
yellow = (255, 255, 102)&lt;br&gt;
black = (0, 0, 0)&lt;br&gt;
red = (213, 50, 80)&lt;br&gt;
green = (0, 255, 0)&lt;br&gt;
blue = (50, 153, 213)&lt;br&gt;
dis_width = 600&lt;br&gt;
dis_height = 400&lt;br&gt;
dis = pygame.display.set_mode((dis_width, dis_height))&lt;br&gt;
pygame.display.set_caption('Snake Game by Pythonist')&lt;br&gt;
clock = pygame.time.Clock()&lt;br&gt;
snake_block = 10&lt;br&gt;
snake_speed = 15&lt;br&gt;
font_style = pygame.font.SysFont("bahnschrift", 25)&lt;br&gt;
score_font = pygame.font.SysFont("comicsansms", 35)&lt;br&gt;
def our_snake(snake_block, snake_list):&lt;br&gt;
    for x in snake_list:&lt;br&gt;
        pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])&lt;br&gt;
def message(msg, color):&lt;br&gt;
    mesg = font_style.render(msg, True, color)&lt;br&gt;
    dis.blit(mesg, [dis_width / 6, dis_height / 3])&lt;br&gt;
def gameLoop():&lt;br&gt;
    game_over = False&lt;br&gt;
    game_close = False&lt;br&gt;
    x1 = dis_width / 2&lt;br&gt;
    y1 = dis_height / 2&lt;br&gt;
    x1_change = 0&lt;br&gt;
    y1_change = 0&lt;br&gt;
    snake_List = []&lt;br&gt;
    Length_of_snake = 1&lt;br&gt;
    foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0&lt;br&gt;
    foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0&lt;br&gt;
    while not game_over:&lt;br&gt;
        while game_close == True:&lt;br&gt;
            dis.fill(blue)&lt;br&gt;
            message("You Lost! Press C-Play Again or Q-Quit", red)&lt;br&gt;
            pygame.display.update()&lt;br&gt;
            for event in pygame.event.get():&lt;br&gt;
                if event.type == pygame.KEYDOWN:&lt;br&gt;
                    if event.key == pygame.K_q:&lt;br&gt;
                        game_over = True&lt;br&gt;
                        game_close = False&lt;br&gt;
                    if event.key == pygame.K_c:&lt;br&gt;
                        gameLoop()&lt;br&gt;
        for event in pygame.event.get():&lt;br&gt;
            if event.type == pygame.QUIT:&lt;br&gt;
                game_over = True&lt;br&gt;
            if event.type == pygame.KEYDOWN:&lt;br&gt;
                if event.key == pygame.K_LEFT:&lt;br&gt;
                    x1_change = -snake_block&lt;br&gt;
                    y1_change = 0&lt;br&gt;
                elif event.key == pygame.K_RIGHT:&lt;br&gt;
                    x1_change = snake_block&lt;br&gt;
                    y1_change = 0&lt;br&gt;
                elif event.key == pygame.K_UP:&lt;br&gt;
                    y1_change = -snake_block&lt;br&gt;
                    x1_change = 0&lt;br&gt;
                elif event.key == pygame.K_DOWN:&lt;br&gt;
                    y1_change = snake_block&lt;br&gt;
                    x1_change = 0&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    if x1 &amp;gt;= dis_width or x1 &amp;lt; 0 or y1 &amp;gt;= dis_height or y1 &amp;lt; 0:
        game_close = True
    x1 += x1_change
    y1 += y1_change
    dis.fill(blue)
    pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block])
    snake_Head = []
    snake_Head.append(x1)
    snake_Head.append(y1)
    snake_List.append(snake_Head)
    if len(snake_List) &amp;gt; Length_of_snake:
        del snake_List[0]
    for x in snake_List[:-1]:
        if x == snake_Head:
            game_close = True
    our_snake(snake_block, snake_List)
    pygame.display.update()
    if x1 == foodx and y1 == foody:
        foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
        foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
        Length_of_snake += 1
    clock.tick(snake_speed)
pygame.quit()
quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;gameLoop()```&lt;br&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  8. Displaying the invoice on the screen
&lt;/h1&gt;

&lt;p&gt;Last but not least, you need to display the player's score. To do this, we created the &lt;strong&gt;Your_score&lt;/strong&gt; function. This function will show the size of the snake minus &lt;strong&gt;1&lt;/strong&gt; (since this is the initial size of the snake).&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import pygame&lt;br&gt;
import time&lt;br&gt;
import random&lt;br&gt;
pygame.init()&lt;br&gt;
white = (255, 255, 255)&lt;br&gt;
yellow = (255, 255, 102)&lt;br&gt;
black = (0, 0, 0)&lt;br&gt;
red = (213, 50, 80)&lt;br&gt;
green = (0, 255, 0)&lt;br&gt;
blue = (50, 153, 213)&lt;br&gt;
dis_width = 600&lt;br&gt;
dis_height = 400&lt;br&gt;
dis = pygame.display.set_mode((dis_width, dis_height))&lt;br&gt;
pygame.display.set_caption('Snake Game by Pythonist')&lt;br&gt;
clock = pygame.time.Clock()&lt;br&gt;
snake_block = 10&lt;br&gt;
snake_speed = 15&lt;br&gt;
font_style = pygame.font.SysFont("bahnschrift", 25)&lt;br&gt;
score_font = pygame.font.SysFont("comicsansms", 35)&lt;br&gt;
def Your_score(score):&lt;br&gt;
    value = score_font.render("Your Score: " + str(score), True, yellow)&lt;br&gt;
    dis.blit(value, [0, 0])&lt;br&gt;
def our_snake(snake_block, snake_list):&lt;br&gt;
    for x in snake_list:&lt;br&gt;
        pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])&lt;br&gt;
def message(msg, color):&lt;br&gt;
    mesg = font_style.render(msg, True, color)&lt;br&gt;
    dis.blit(mesg, [dis_width / 6, dis_height / 3])&lt;br&gt;
def gameLoop():&lt;br&gt;
    game_over = False&lt;br&gt;
    game_close = False&lt;br&gt;
    x1 = dis_width / 2&lt;br&gt;
    y1 = dis_height / 2&lt;br&gt;
    x1_change = 0&lt;br&gt;
    y1_change = 0&lt;br&gt;
    snake_List = []&lt;br&gt;
    Length_of_snake = 1&lt;br&gt;
    foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0&lt;br&gt;
    foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0&lt;br&gt;
    while not game_over:&lt;br&gt;
        while game_close == True:&lt;br&gt;
            dis.fill(blue)&lt;br&gt;
            message("You Lost! Press C-Play Again or Q-Quit", red)&lt;br&gt;
            Your_score(Length_of_snake - 1)&lt;br&gt;
            pygame.display.update()&lt;br&gt;
            for event in pygame.event.get():&lt;br&gt;
                if event.type == pygame.KEYDOWN:&lt;br&gt;
                    if event.key == pygame.K_q:&lt;br&gt;
                        game_over = True&lt;br&gt;
                        game_close = False&lt;br&gt;
                    if event.key == pygame.K_c:&lt;br&gt;
                        gameLoop()&lt;br&gt;
        for event in pygame.event.get():&lt;br&gt;
            if event.type == pygame.QUIT:&lt;br&gt;
                game_over = True&lt;br&gt;
            if event.type == pygame.KEYDOWN:&lt;br&gt;
                if event.key == pygame.K_LEFT:&lt;br&gt;
                    x1_change = -snake_block&lt;br&gt;
                    y1_change = 0&lt;br&gt;
                elif event.key == pygame.K_RIGHT:&lt;br&gt;
                    x1_change = snake_block&lt;br&gt;
                    y1_change = 0&lt;br&gt;
                elif event.key == pygame.K_UP:&lt;br&gt;
                    y1_change = -snake_block&lt;br&gt;
                    x1_change = 0&lt;br&gt;
                elif event.key == pygame.K_DOWN:&lt;br&gt;
                    y1_change = snake_block&lt;br&gt;
                    x1_change = 0&lt;br&gt;
        if x1 &amp;gt;= dis_width or x1 &amp;lt; 0 or y1 &amp;gt;= dis_height or y1 &amp;lt; 0:&lt;br&gt;
            game_close = True&lt;br&gt;
        x1 += x1_change&lt;br&gt;
        y1 += y1_change&lt;br&gt;
        dis.fill(blue)&lt;br&gt;
        pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block])&lt;br&gt;
        snake_Head = []&lt;br&gt;
        snake_Head.append(x1)&lt;br&gt;
        snake_Head.append(y1)&lt;br&gt;
        snake_List.append(snake_Head)&lt;br&gt;
        if len(snake_List) &amp;gt; Length_of_snake:&lt;br&gt;
            del snake_List[0]&lt;br&gt;
        for x in snake_List[:-1]:&lt;br&gt;
            if x == snake_Head:&lt;br&gt;
                game_close = True&lt;br&gt;
        our_snake(snake_block, snake_List)&lt;br&gt;
        Your_score(Length_of_snake - 1)&lt;br&gt;
        pygame.display.update()&lt;br&gt;
        if x1 == foodx and y1 == foody:&lt;br&gt;
            foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0&lt;br&gt;
            foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0&lt;br&gt;
            Length_of_snake += 1&lt;br&gt;
        clock.tick(snake_speed)&lt;br&gt;
    pygame.quit()&lt;br&gt;
    quit()&lt;br&gt;
gameLoop()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>python</category>
      <category>gamedev</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to raise to a degree in C++?</title>
      <dc:creator>Ilya</dc:creator>
      <pubDate>Sat, 06 Nov 2021 19:27:36 +0000</pubDate>
      <link>https://dev.to/ilya_romanov/how-to-raise-to-a-degree-in-c-2cp8</link>
      <guid>https://dev.to/ilya_romanov/how-to-raise-to-a-degree-in-c-2cp8</guid>
      <description>&lt;p&gt;To raise a number to a power in C++, it is enough to use the pow function, which is located in the math.h library.&lt;/p&gt;

&lt;p&gt;The pow function takes two arguments: a number for exponentiation and an exponent of&lt;br&gt;
double pow (double base, double exponent);&lt;/p&gt;

&lt;p&gt;Sample code:&lt;/p&gt;

&lt;h2&gt;
  
  
  include 
&lt;/h2&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;p&gt;using namespace std;&lt;/p&gt;

&lt;p&gt;int main ()&lt;br&gt;
{&lt;br&gt;
printf ("%g",pow(3,5));&lt;br&gt;
return 0;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;The result of this program will be the output of the number 243, which is 3 to the 5th power.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
