|
| 1 | +importpygame |
| 2 | +importrandom |
| 3 | + |
| 4 | +# setting up some initial parameters |
| 5 | +WIDTH,HEIGHT=600,600 |
| 6 | +BLOCK_SIZE=20 |
| 7 | + |
| 8 | +pygame.font.init() |
| 9 | +score_font=pygame.font.SysFont("consolas",20)# or any other font you'd like |
| 10 | +score=0 |
| 11 | + |
| 12 | +# color definition |
| 13 | +WHITE= (255,255,255) |
| 14 | +RED= (255,0,0) |
| 15 | + |
| 16 | +# initialize pygame |
| 17 | +pygame.init() |
| 18 | + |
| 19 | +# setting up display |
| 20 | +win=pygame.display.set_mode((WIDTH,HEIGHT)) |
| 21 | + |
| 22 | +# setting up clock |
| 23 | +clock=pygame.time.Clock() |
| 24 | + |
| 25 | +# snake and food initialization |
| 26 | +snake_pos= [[WIDTH//2,HEIGHT//2]] |
| 27 | +snake_speed= [0,BLOCK_SIZE] |
| 28 | + |
| 29 | +teleport_walls=True# set this to True to enable wall teleporting |
| 30 | + |
| 31 | + |
| 32 | +defgenerate_food(): |
| 33 | +whileTrue: |
| 34 | +x=random.randint(0, (WIDTH-BLOCK_SIZE)//BLOCK_SIZE )*BLOCK_SIZE |
| 35 | +y=random.randint(0, (HEIGHT-BLOCK_SIZE)//BLOCK_SIZE )*BLOCK_SIZE |
| 36 | +food_pos= [x,y] |
| 37 | +iffood_posnotinsnake_pos: |
| 38 | +returnfood_pos |
| 39 | + |
| 40 | +food_pos=generate_food() |
| 41 | + |
| 42 | +defdraw_objects(): |
| 43 | +win.fill((0,0,0)) |
| 44 | +forposinsnake_pos: |
| 45 | +pygame.draw.rect(win,WHITE,pygame.Rect(pos[0],pos[1],BLOCK_SIZE,BLOCK_SIZE)) |
| 46 | +pygame.draw.rect(win,RED,pygame.Rect(food_pos[0],food_pos[1],BLOCK_SIZE,BLOCK_SIZE)) |
| 47 | +# Render the score |
| 48 | +score_text=score_font.render(f"Score:{score}",True,WHITE) |
| 49 | +win.blit(score_text, (10,10))# draws the score on the top-left corner |
| 50 | + |
| 51 | + |
| 52 | +defupdate_snake(): |
| 53 | +globalfood_pos,score |
| 54 | +new_head= [snake_pos[0][0]+snake_speed[0],snake_pos[0][1]+snake_speed[1]] |
| 55 | + |
| 56 | +ifteleport_walls: |
| 57 | +# if the new head position is outside of the screen, wrap it to the other side |
| 58 | +ifnew_head[0]>=WIDTH: |
| 59 | +new_head[0]=0 |
| 60 | +elifnew_head[0]<0: |
| 61 | +new_head[0]=WIDTH-BLOCK_SIZE |
| 62 | +ifnew_head[1]>=HEIGHT: |
| 63 | +new_head[1]=0 |
| 64 | +elifnew_head[1]<0: |
| 65 | +new_head[1]=HEIGHT-BLOCK_SIZE |
| 66 | + |
| 67 | +ifnew_head==food_pos: |
| 68 | +food_pos=generate_food()# generate new food |
| 69 | +score+=1# increment score when food is eaten |
| 70 | +else: |
| 71 | +snake_pos.pop()# remove the last element from the snake |
| 72 | + |
| 73 | +snake_pos.insert(0,new_head)# add the new head to the snake |
| 74 | + |
| 75 | + |
| 76 | +defgame_over(): |
| 77 | +# game over when snake hits the boundaries or runs into itself |
| 78 | +ifteleport_walls: |
| 79 | +returnsnake_pos[0]insnake_pos[1:] |
| 80 | +else: |
| 81 | +returnsnake_pos[0]insnake_pos[1:]or \ |
| 82 | +snake_pos[0][0]>WIDTH-BLOCK_SIZEor \ |
| 83 | +snake_pos[0][0]<0or \ |
| 84 | +snake_pos[0][1]>HEIGHT-BLOCK_SIZEor \ |
| 85 | +snake_pos[0][1]<0 |
| 86 | + |
| 87 | + |
| 88 | +defgame_over_screen(): |
| 89 | +globalscore |
| 90 | +win.fill((0,0,0)) |
| 91 | +game_over_font=pygame.font.SysFont("consolas",50) |
| 92 | +game_over_text=game_over_font.render(f"Game Over! Score:{score}",True,WHITE) |
| 93 | +win.blit(game_over_text, (WIDTH//2-game_over_text.get_width()//2,HEIGHT//2-game_over_text.get_height()//2)) |
| 94 | +pygame.display.update() |
| 95 | + |
| 96 | +whileTrue: |
| 97 | +foreventinpygame.event.get(): |
| 98 | +ifevent.type==pygame.QUIT: |
| 99 | +pygame.quit() |
| 100 | +return |
| 101 | +ifevent.type==pygame.KEYDOWN: |
| 102 | +ifevent.key==pygame.K_r: |
| 103 | +run()# replay the game |
| 104 | +return |
| 105 | +elifevent.key==pygame.K_q: |
| 106 | +pygame.quit()# quit the game |
| 107 | +return |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +defrun(): |
| 112 | +globalsnake_speed,snake_pos,food_pos,score |
| 113 | +snake_pos= [[WIDTH//2,HEIGHT//2]] |
| 114 | +snake_speed= [0,BLOCK_SIZE] |
| 115 | +food_pos=generate_food() |
| 116 | +score=0 |
| 117 | +running=True |
| 118 | +whilerunning: |
| 119 | +foreventinpygame.event.get(): |
| 120 | +ifevent.type==pygame.QUIT: |
| 121 | +running=False |
| 122 | +keys=pygame.key.get_pressed() |
| 123 | +forkeyinkeys: |
| 124 | +ifkeys[pygame.K_UP]: |
| 125 | +# when UP is pressed but the snake is moving down, ignore the input |
| 126 | +ifsnake_speed[1]==BLOCK_SIZE: |
| 127 | +continue |
| 128 | +snake_speed= [0,-BLOCK_SIZE] |
| 129 | +ifkeys[pygame.K_DOWN]: |
| 130 | +# when DOWN is pressed but the snake is moving up, ignore the input |
| 131 | +ifsnake_speed[1]==-BLOCK_SIZE: |
| 132 | +continue |
| 133 | +snake_speed= [0,BLOCK_SIZE] |
| 134 | +ifkeys[pygame.K_LEFT]: |
| 135 | +# when LEFT is pressed but the snake is moving right, ignore the input |
| 136 | +ifsnake_speed[0]==BLOCK_SIZE: |
| 137 | +continue |
| 138 | +snake_speed= [-BLOCK_SIZE,0] |
| 139 | +ifkeys[pygame.K_RIGHT]: |
| 140 | +# when RIGHT is pressed but the snake is moving left, ignore the input |
| 141 | +ifsnake_speed[0]==-BLOCK_SIZE: |
| 142 | +continue |
| 143 | +snake_speed= [BLOCK_SIZE,0] |
| 144 | + |
| 145 | +draw_objects() |
| 146 | +update_snake() |
| 147 | +ifgame_over(): |
| 148 | +game_over_screen() |
| 149 | +break |
| 150 | +pygame.display.update() |
| 151 | +clock.tick(10) |
| 152 | +pygame.quit() |
| 153 | + |
| 154 | + |
| 155 | +if__name__=='__main__': |
| 156 | +run() |