Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jarand0905 committed Jun 1, 2023
0 parents commit b373458
Show file tree
Hide file tree
Showing 219 changed files with 36,904 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
130 changes: 130 additions & 0 deletions 8_ball/8ball.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import pygame as pg
import pymunk

import math

# Initialiserer/starter pygame
pg.init()

# Oppretter et vindu der vi skal "tegne" innholdet vårt
VINDU_BREDDE = 400
VINDU_HOYDE = 500
vindu = pg.display.set_mode([VINDU_BREDDE, VINDU_HOYDE])


class Ball:
"""Klasse for å representere en ball"""
def __init__(self, x, y, xfart, yfart, radius, vindusobjekt):
"""Konstruktør"""
self.x = x
self.y = y
self.xfart = xfart
self.yfart = yfart
self.radius = radius
self.vindusobjekt = vindusobjekt

def tegn_sirkel(self):
"""Metode for å tegne ballen"""
pg.draw.circle(self.vindusobjekt, (255, 69, 0), (self.x, self.y), self.radius)

def flytt_x(self):
"""Metode for å flytte ballen"""
# Sjekker om ballen er utenfor høyre/venstre kant
if ((self.x - self.radius) <= 0) or ((self.x + self.radius) >= self.vindusobjekt.get_width()):
self.xfart = -self.xfart

# Flytter ballen
self.x += self.xfart

def flytt_y(self):
"""Metode for å flytte ballen"""
# Sjekker om ballen er utenfor høyre/venstre kant
if ((self.y - self.radius) <= 0) or ((self.y + self.radius) >= self.vindusobjekt.get_height()):
self.yfart = -self.yfart
# Flytter ballen
self.y += self.yfart

class Linje:
def __init__(self, r, g, b, x_start, y_start, x_slutt, y_slutt):
self.r = r
self.g = g
self.b = b
self.x_start = x_start
self.y_start = y_start
self.x_slutt = x_slutt
self.y_slutt = y_slutt


def tegn_linje(self):
"tegner linjen"
pg.draw.line(vindu, (self.r, self.g, self.b), [self.x_start, self.y_start], [self.x_slutt, self.y_slutt], 3)


# Lager et Ball-objekt
ball = Ball(210, 250, 0, 0, 15, vindu)

friksjon = 0.01


# Gjenta helt til brukeren lukker vinduet
fortsett = True
while fortsett:
pg.time.Clock().tick(120)
# Sjekker om brukeren har lukket vinduet
for event in pg.event.get():
if event.type == pg.QUIT:
fortsett = False

#friksjon
if ball.xfart > 0:
ball.xfart = abs(ball.xfart) - friksjon
if ball.yfart > 0:
ball.yfart = abs(ball.yfart) - friksjon

#mouse position
mouse_position_x = pg.mouse.get_pos()[0]
mouse_position_y = pg.mouse.get_pos()[1]

#linje objekt
linje = Linje(200,200,200, ball.x, ball.y, mouse_position_x, mouse_position_y )


#registerer museklikk
#fikse nestegang at man kan holde inne
left, middle, right = pg.mouse.get_pressed()
if left:
linje.g = 0
linje.b = 0
lengde = math.sqrt((abs(linje.x_slutt) - abs(linje.x_start))**2 + (abs(linje.y_slutt) - abs(linje.y_start))**2)
ball.xfart = lengde/100
ball.yfart =lengde/100



# Farger bakgrunnen lyseblå
vindu.fill((0, 100, 0))

# Tegner og flytter ballen
ball.tegn_sirkel()
ball.flytt_x()
ball.flytt_y()

linje.tegn_linje()


#pg.draw.rect(vindu, (100, 0, 0), (ball.x, ball.y, 10, mouse_position))


pg.draw.circle(vindu, (0, 0, 0), (13, 15), 15)
pg.draw.circle(vindu, (0, 0, 0), (13, 242), 15)
pg.draw.circle(vindu, (0, 0, 0), (13, 485), 15)
#Høyre_hull
pg.draw.circle(vindu, (0, 0, 0), (387, 15), 15)
pg.draw.circle(vindu, (0, 0, 0), (387, 242), 15)
pg.draw.circle(vindu, (0, 0, 0), (387, 485), 15)

# Oppdaterer alt innholdet i vinduet
pg.display.flip()

# Avslutter pygame
pg.quit()
128 changes: 128 additions & 0 deletions 8_ball/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import sys

import pygame
from pygame.locals import *

import pymunk

class Linje:
def __init__(self, r, g, b, x_start, y_start, x_slutt, y_slutt):
self.r = r
self.g = g
self.b = b
self.x_start = x_start
self.y_start = y_start
self.x_slutt = x_slutt
self.y_slutt = y_slutt


def tegn_linje(self):
"tegner linjen"
pygame.draw.line(screen, (self.r, self.g, self.b), [self.x_start, self.y_start], [self.x_slutt, self.y_slutt], 3)


def create_Ball(space,height,width):
body=pymunk.Body(1,100,body_type = pymunk.Body.DYNAMIC)
body.position = (height,width)
shape = pymunk.Circle(body,10)
shape.elasticity = 1
#body.velocity = (200,200)
space.add(body,shape)
return shape

def draw_balls(balls):
for ball in balls:
pos_x = int(ball.body.position.x)
pos_y = int(ball.body.position.y)
pygame.draw.circle(screen,(255,255,255),(pos_x,pos_y),10)
"""
def create_wall(space):
body = pymunk.Body(1 , 100 , body_type = pymunk.Body.STATIC)
body.position = (width,height)
shape = pymunk.Segment(body, (0, height), (650, height), 5)
space.add(body,shape)
return shape
def draw_wall(walls):
for wall in walls:
pos_x = wall.body.position.x
pos_y = wall.body.position.y
print(pos_x)
pygame.draw.line(screen,(0,0,0),(pos_x,pos_y),1)
"""
class Box:
def __init__(self, p0=(0, 0), p1=(640, 480), d=2):
self.p0 = p0
self.p1 = p1
x0, y0 = p0
x1, y1 = p1
self.pts = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
for i in range(4):
segment = pymunk.Segment(space.static_body, self.pts[i], self.pts[(i+1)%4], d)
segment.elasticity = 0.5
segment.friction = 1
space.add(segment)



pygame.init()

fps = 60
fpsClock = pygame.time.Clock()

width, height = 640, 480
screen = pygame.display.set_mode((width, height))

space = pymunk.Space()
space.gravity = (0,0)

balls = []
balls.append(create_Ball(space,200,100))
balls.append(create_Ball(space,250,400))



walls = []
box=Box()
#walls.append(create_wall(space))
# Game loop.
while True:
screen.fill((255, 255, 255))

for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
balls[1].body.velocity = (0,500)

balls[1].body.velocity = balls[1].body.velocity * 0.99
if abs(balls[0].body.velocity[0]) < 5 and abs(balls[0].body.velocity[1]) < 5:
balls[0].body.velocity = (0,0)


# Update.
mouse_position_x = pygame.mouse.get_pos()[0]
mouse_position_y = pygame.mouse.get_pos()[1]

print(balls[0].body.position)
screen.fill((0, 100, 0))
pygame.draw.circle(screen, (0, 0, 0), (13, 15), 15)
pygame.draw.circle(screen, (0, 0, 0), (13, 242), 15)
pygame.draw.circle(screen, (0, 0, 0), (13, 460), 15)
#Høyre_hull
pygame.draw.circle(screen, (0, 0, 0), (620, 15), 15)
pygame.draw.circle(screen, (0, 0, 0), (620, 242), 15)
pygame.draw.circle(screen, (0, 0, 0), (620, 460), 15)


# Draw.
linje = Linje(200,200,200, balls[1].body.position.x, balls[1].body.position.y, mouse_position_x, mouse_position_y)
linje.tegn_linje()

draw_balls(balls)
pygame.draw.rect(screen,(0,0,0),(box.p0+box.p1),6)
#draw_wall(walls)
space.step(1/50)
fpsClock.tick(fps)
pygame.display.flip()
Loading

0 comments on commit b373458

Please sign in to comment.