Skip to content

Commit

Permalink
snake sent to capm, orientation is now correct
Browse files Browse the repository at this point in the history
  • Loading branch information
nlkguy committed Dec 19, 2023
1 parent a513ceb commit 306deed
Showing 1 changed file with 86 additions and 18 deletions.
104 changes: 86 additions & 18 deletions cg-lab/snakelangelo.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@
from OpenGL.GL import*
from OpenGL.GLU import*
from OpenGL.GLUT import*
import math , time , random

WINDOWSIZE=1000

timer = 1
import math , random
"""
- random apple(a single lonely one) appear on screen in (appleX,appleY) coordinates
- snake boi is in an arbitrary position (snakeX,snakeY) coordinates
- function finds a way to go eat the apple
- no.of segments in the snake increase
- new apple (still single and lonely) appears
"""
WINDOWSIZE=1000 # window size
timer = 1 # reference timer

appleX , appleY , appleR = 0,0,50
eaten = False # F - not eaten, T - eaten

"""
------ ------
| | |
| 2 | 1 |
------ ------
| | |
| 3 | 4 |
------ ------
"""
SnakeQuadrant = 1
appleQuadrant = 1


snakeX,snakeY = -900,-900 # snake coordinates
tailX,tailY = 0,0 # tail coordinates
snakeSegCount = 1 # number of segments of snake
snakeSize = 50 # snake size

direction = 0 # snake direction
"""
- random apple(a single lonely one) appear on screen in (appleX,appleY) coordinates
- snake boi is in an arbitrary position (snakeX,snakeY) coordinates
- function finds a way to go eat the apple
- no.of segments in the snake increase
- new apple (still single and lonely) appears
-----
1 - Up | |
2 - Down | 1 |
-----------------
3 - Right | | | | | |
4 - Left | 4 | | 2 | | 3 |
-----------------
"""




def grid(WINDOWSIZE=1000,offset=100):
glColor3f(0.5,0.5,0.5)
Expand All @@ -40,6 +63,7 @@ def grid(WINDOWSIZE=1000,offset=100):

glEnd()


def apple_coordinates():
global appleX,appleY,eaten
# get random apple coordinates
Expand All @@ -59,39 +83,70 @@ def apple_putter():
glVertex2f(appleR*math.cos(math.pi*i/180)+appleX,
appleR*math.sin(math.pi*i/180)+appleY)
glEnd()
print(f"put-ed apple at ({appleX},{appleY})")
#print(f"put-ed apple at ({appleX},{appleY})")


def find_apple_quadrant():
global appleX,appleY,snakeX,snakeY
global SnakeQuadrant,appleQuadrant

if appleX>0:
if appleY>0:
appleQuadrant = 1
elif appleY<0:
appleQuadrant = 4
if appleX<0:
if appleY>0:
appleQuadrant = 2
elif appleY<0:
appleQuadrant = 3

print(f"apple at quadrant ({appleQuadrant})")
print(f"\t\t snake direction ({direction})")



def snake():
global tailX,tailY
glColor3f(0.1,1,1) #magenta? violet? idk
glPointSize(snakeSize)
glBegin(GL_POINTS)
print(f"snake at ({snakeX},{snakeY})")
glVertex2f(snakeX,snakeY)
glEnd()
tailX=snakeX
tailY=snakeY

for i in range(1,snakeSegCount):
glColor3f(0.1,1,0) #magenta? violet? idk
glPointSize(snakeSize)
glBegin(GL_POINTS)
print(f"snake at ({snakeX},{snakeY})")
glVertex2f(snakeX-(i*110),snakeY)
#print(f"snake at ({snakeX},{snakeY})")
#glVertex2f(tailX-(i*110),tailY)
if direction == 1:
glVertex2f(tailX,tailY-(i*110))
elif direction == 2:
glVertex2f(tailX,tailY+(i*110))
elif direction == 3:
glVertex2f(tailX-(i*110),tailY)
elif direction == 4:
glVertex2f(tailX+(i*110),tailY)
glEnd()





def animate(temp):

global timer
global appleX,appleY,appleR,eaten
global snakeX,snakeY,snakeSegCount,snakeSize
global tailX,tailY,oldX,oldY,direction



glutPostRedisplay()
glutTimerFunc(int(5000/60),animate,int(0))
glutTimerFunc(int(50000/60),animate,int(0))

timer+=1
print(timer)
Expand All @@ -100,20 +155,33 @@ def animate(temp):

if eaten:
apple_coordinates()

find_apple_quadrant()

if snakeX<appleX:
oldX = snakeX
snakeX+=100
direction = 3
elif snakeX>appleX:
oldX = snakeX
snakeX-=100
direction = 4
elif snakeX==appleX:
if snakeY<appleY:
oldY = snakeY
snakeY+=100
direction = 1

elif snakeY>appleY:
oldY = snakeY
snakeY-=100
direction = 2

elif snakeY==appleY:
eaten = True
snakeSegCount+=1
print(f"snake segment count ({snakeSegCount})")
print(f"\t\tsnake segment count ({snakeSegCount})")
print(f"\t\tapple at quadrant ({appleQuadrant})")
print(f"direction : {direction}")



Expand Down

0 comments on commit 306deed

Please sign in to comment.