Skip to content

Instantly share code, notes, and snippets.

@Torxed
Created March 20, 2019 20:29
Show Gist options
  • Save Torxed/ea0efc8f2ca3202316905f6e8e5bdb87 to your computer and use it in GitHub Desktop.
Save Torxed/ea0efc8f2ca3202316905f6e8e5bdb87 to your computer and use it in GitHub Desktop.
import pyglet
import random
pyglet.resource.path = ["resources"]
pyglet.resource.reindex()
# sets the resource path
class Snake_Window(pyglet.window.Window):
a = 0
dtx = 160
dty = 200
# sets the basic direction and snake body x and y
def __init__(self):
super(Snake_Window, self).__init__(width=1280, height=720)
# sets resolution and inherits
self.key_handler = pyglet.window.key.KeyStateHandler()
self.push_handlers(self.key_handler)
# sets keys
self.set_caption("Wild Snake")
# gives it a name
self.background_image = pyglet.resource.image("background.png")
self.food_image = pyglet.resource.image("food.png")
self.snake_head_image = pyglet.resource.image("snake_head.png")
self.snake_body_image = pyglet.resource.image("snake_body.png")
# makes images usable
self.center_image(self.food_image)
self.center_image(self.snake_head_image)
self.center_image(self.snake_body_image)
# centers the images using center_image
self.snake_head = pyglet.sprite.Sprite(img=self.snake_head_image, x=200, y=200)
self.snake_head.rotation = 270
# sets snake_head as a image on screen
self.snake_body = pyglet.sprite.Sprite(img=self.snake_body_image, x=self.dtx, y=self.dty)
self.snake_body.scale = 0.1
self.snake_body.rotation = 90
# sets snake_body as a image on screen
self.background = pyglet.sprite.Sprite(img=self.background_image, x=0, y=0)
# sets background as a image on screen
self.food = []
# sets food
pyglet.clock.schedule_interval(self.game_tick, 0.1)
def center_image(self, image):
# sets the center of the image to the actual center
image.anchor_x = image.width / 2
image.anchor_y = image.height / 2
def update_snake_head(self):
# makes the snake head go and sets the x and y for the body
if self.a == 0:
self.snake_head.x += 40
self.dtx = self.snake_head.x - 40
self.dty = self.snake_head.y
elif self.a == 1:
self.snake_head.x -= 40
self.dtx = self.snake_head.x + 40
self.dty = self.snake_head.y
elif self.a == 2:
self.snake_head.y += 40
self.dty = self.snake_head.y - 40
self.dtx = self.snake_head.x
elif self.a == 3:
self.snake_head.y -= 40
self.dty = self.snake_head.y + 40
self.dtx = self.snake_head.x
def update_snake_body(self, dtx, dty):
# makes the snakes body go
self.snake_body.x = dtx
self.snake_body.y = dty
def game_tick(self, dt):
# updates snakes head, snakes body, key presses and sets the background
self.update_snake_head()
self.update_snake_body(self.dtx, self.dty)
self.draw_elements()
self.key_press()
def draw_elements(self):
# draws everything in window
self.clear()
self.background.draw()
print('Head:', self.snake_head.x, self.snake_head.y, {0:'Right', 1:'Left', 2: 'Up', 3:'Down'}[self.a])
self.snake_head.draw()
self.snake_body.draw()
self.flip()
def key_press(self):
# sets direction of snake upon key press and rotates his head accordingly
if self.key_handler[pyglet.window.key.SPACE]:
print('Here')
if self.key_handler[pyglet.window.key.RIGHT]:
if self.a == 1:
pass
else:
self.a = 0
self.snake_head.rotation = 270
elif self.key_handler[pyglet.window.key.LEFT]:
if self.a == 0:
pass
else:
self.a = 1
self.snake_head.rotation = 90
elif self.key_handler[pyglet.window.key.UP]:
if self.a == 3:
pass
else:
self.a = 2
self.snake_head.rotation = 180
elif self.key_handler[pyglet.window.key.DOWN]:
if self.a == 2:
pass
else:
self.a = 3
self.snake_head.rotation = 0
game_window = Snake_Window()
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment