Skip to content

Commit

Permalink
Assignment 01 and Assignment 02
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed Sep 23, 2020
0 parents commit f641035
Show file tree
Hide file tree
Showing 22 changed files with 2,069 additions and 0 deletions.
129 changes: 129 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "2D Game",
"type": "python",
"request": "launch",
"program": "a02_2d-game/main.py",
"console": "integratedTerminal"
}
]
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 xFrednet

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Empty file added a01/__init__.py
Empty file.
101 changes: 101 additions & 0 deletions a01/entities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import glm
import pygame
import OpenGL.GL as gl

class Entity:
def __init__(
self,
position = glm.vec2(0, 0),
color = glm.vec3(0.0, 0.0, 0.0),
size = 10
):
self.position = position
self.color = color
self.size = size

def update(self, _delta):
pass

def draw(self):
step = self.size / 2

# 0 3---5
# | \ \ |
# 1---2 4

gl.glColor3f(self.color.x, self.color.y, self.color.z)

gl.glBegin(gl.GL_TRIANGLES)
gl.glVertex2f(self.position.x - step, self.position.y - step)
gl.glVertex2f(self.position.x - step, self.position.y + step)
gl.glVertex2f(self.position.x + step, self.position.y + step)

gl.glVertex2f(self.position.x - step, self.position.y - step)
gl.glVertex2f(self.position.x + step, self.position.y + step)
gl.glVertex2f(self.position.x + step, self.position.y - step)
gl.glEnd()

class BouncingBall(Entity):
# Also known as ScreenSaverBall

def __init__(
self,
area,
position = glm.vec2(0, 0),
velocity = glm.vec2(0, 0),
color = glm.vec3(0.0, 0.0, 0.0),
size = 10):
Entity.__init__(
self,
position=position,
color=color,
size=size)

self.velocity = velocity
self.area = area

def update(self, delta):
self.position = self.position + self.velocity * delta

halve_size = self.size / 2
if ((self.position.x - halve_size) < 0 or self.position.x + halve_size >= self.area.x):
self.velocity.x *= -1
if ((self.position.y - halve_size) < 0 or self.position.y + halve_size>= self.area.y):
self.velocity.y *= -1

class ControlledBall(Entity):
def __init__(
self,
area,
position = glm.vec2(0.0, 0.0),
speed = 100.0,
color = glm.vec3(0.0, 0.0, 0.0),
size = 10):
Entity.__init__(
self,
position=position,
color=color,
size=size)

self.speed = speed
self.area = area

def update(self, delta):
keys = pygame.key.get_pressed()

movement = glm.vec2()
if keys[pygame.locals.K_LEFT]:
movement.x = -1.0
if keys[pygame.locals.K_RIGHT]:
movement.x = +1.0
if keys[pygame.locals.K_UP]:
movement.y = +1.0
if keys[pygame.locals.K_DOWN]:
movement.y = -1.0

glm.normalize(movement)

size_padding = glm.vec2(self.size / 2, self.size / 2)

self.position = self.position + (movement * self.speed * delta)
self.position = glm.clamp(self.position, size_padding, self.area- size_padding)
103 changes: 103 additions & 0 deletions a01/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import pygame
from pygame.locals import *

from OpenGL import GL as gl
import OpenGL.GLU as glu

import random

import glm

import entities
from entities import BouncingBall
from entities import ControlledBall

BOUNCING_BALL_COUNT = 100
CONTROLLED_BALL_COUNT = 100

FPS = 60
RESOLUTION = glm.vec2(800, 600)

entities = []

def init_game():
pygame.display.init()
pygame.display.set_mode((int(RESOLUTION.x), int(RESOLUTION.y)), DOUBLEBUF|OPENGL)
gl.glClearColor(0.1, 0.1, 0.1, 1.0)

for _ in range(0, BOUNCING_BALL_COUNT):
entities.append(
BouncingBall(
RESOLUTION,
position=glm.vec2(random.uniform(10.0, RESOLUTION.x - 10.0), random.uniform(10.0, RESOLUTION.y - 10.0)),
velocity=glm.vec2(random.uniform(-100.0, 100.0), random.uniform(-100.0, 100.0)),
color=glm.vec3(1.0, 0.0, 0.0)))

for _ in range(0, CONTROLLED_BALL_COUNT):
entities.append(
ControlledBall(
RESOLUTION,
position=glm.vec2(random.uniform(10.0, RESOLUTION.x - 10.0), random.uniform(10.0, RESOLUTION.y - 10.0)),
speed=100,
color=glm.vec3(0.0, 1.0, 0.0)))

def update(delta):
for e in entities:
e.update(delta)

def display():
gl.glClear(gl.GL_COLOR_BUFFER_BIT)

gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()

gl.glMatrixMode(gl.GL_MODELVIEW)
gl.glLoadIdentity()

gl.glViewport(0, 0, int(RESOLUTION.x), int(RESOLUTION.y))
glu.gluOrtho2D(0, int(RESOLUTION.x), 0, int(RESOLUTION.y))

for e in entities:
e.draw()

pygame.display.flip()

def game_loop():

clock = pygame.time.Clock()
last_millis = pygame.time.get_ticks()

while True:
# Delta timing. See https://en.wikipedia.org/wiki/Delta_timing
# Trust me, this gets important in larger games
# Pygame implementation stolen from: https://stackoverflow.com/questions/24039804/pygame-current-time-millis-and-delta-time
millis = pygame.time.get_ticks()
delta = (millis - last_millis) / 1000.0
last_millis = millis

# Get events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit(0)
elif event.type == pygame.KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
quit(0)
elif event.type == pygame.locals.MOUSEBUTTONDOWN:
entities.append(
BouncingBall(
RESOLUTION,
position=glm.vec2(float(event.pos[0]), RESOLUTION.y - float(event.pos[1])),
velocity=glm.vec2(),
color=glm.vec3(0.0, 0.0, 1.0)))

# Update
update(delta)
display()

clock.tick(FPS)

if __name__ == "__main__":
init_game()
game_loop()
3 changes: 3 additions & 0 deletions a01_without_pygame/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .shader_program import *
from .vertex_buffer import *
from .render_systems import *
Loading

0 comments on commit f641035

Please sign in to comment.