Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, sys
- from math import *
- heroPos = 209
- clock = pygame.time.Clock()
- screen = pygame.display.set_mode([720,540])
- import random
- cells = []
- hey = bool
- pos = []
- applePos = random.randint(1,401)
- for x in range(100):
- pos.append(0)
- lenght = 0
- clock = pygame.time.Clock()
- dir = 0
- for column in range(20):
- for line in range(20):
- cells.append([51 + 22 * column, 51 + 22 * line, 21, 21])
- while True:
- if heroPos == applePos:
- lenght += 1
- applePos = random.randint(0, 401)
- screen.fill((0,0,0))
- pygame.draw.rect(screen, [0, 255, 0], cells[applePos])
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit(0)
- if event.type == pygame.KEYDOWN and event.key == pygame.K_d:
- lenght += 1
- if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
- hey = True
- if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT and not dir == 4:
- dir = 2
- if event.type == pygame.KEYDOWN and event.key == pygame.K_UP and dir != 3:
- dir = 1
- if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN and dir != 1:
- dir = 3
- if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT and dir != 2:
- dir = 4
- pygame.draw.rect(screen, [255 , 255, 255], [50, 50, 440, 440], 3)
- for x in range(20):
- pygame.draw.line(screen,[255,255,255], [50 + 22 * x, 50], [50 + 22 * x, 490], 1)
- for x in range(20):
- pygame.draw.line(screen,[255,255,255], [50, 50 + 22 * x], [490, 50 + 22 * x], 1)
- if hey == True:
- if True:
- clock.tick(8)
- if dir == 1:
- if heroPos % 20 != 0:
- heroPos -= 1
- else:
- print("stop")
- dir = 0
- hey = False
- heroPos = 209
- lenght = 0
- if dir == 2:
- if heroPos < 380:
- heroPos += 20
- else:
- print("stop")
- dir = 0
- hey = False
- heroPos = 209
- lenght = 0
- if dir == 3:
- if heroPos % 20 != 19:
- heroPos += 1
- else:
- print("stop")
- dir = 0
- hey = False
- heroPos = 209
- lenght = 0
- if dir == 4:
- if heroPos > 20:
- heroPos -= 20
- else:
- print("stop")
- dir = 0
- hey = False
- heroPos = 209
- lenght = 0
- if dir != 0:
- pos.insert(0, heroPos)
- pos.pop()
- for x in range(lenght):
- pygame.draw.rect(screen, [255, 0, 0], cells[pos[x + 1]])
- if pos[x + 1] == heroPos:
- print("stop")
- dir = 0
- hey = False
- heroPos = 209
- lenght = 0
- pygame.draw.rect(screen, [255, 0, 0], cells[heroPos])
- pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement