Ég var að æfa mig í oop í pygame og bjó til class sem býr til kassa sem skoppa auk þess sem þeir hafa hröðun.

Núna vantar mig utanaðkomandi álit hvers vegna kassarnir mínir hoppa mishátt (ættu alltaf að hoppa aftur upp á byrjunarreit).

Tek vel á móti hjálplegri gagnrýni.

import pygame
from pygame.locals import *
class AcceleratingBox(object):
    def __init__(self, position, dimension = (15,15), color = (255,255,255), gravity = 9.81):
        self.dim = dimension
        self.color = color
        self.gravity = float(gravity)
        self.speed = 0
        self.pos = position
        self.surf = pygame.Surface(self.dim)
        self.surf.fill(self.color)
        
        
    def changeDirection(self):
        self.speed *= -1
        
    def pull(self,power):
        self.speed += power
        
    def move(self):
        self.pos = (self.pos[0],self.pos[1]+self.speed)
        
    def Rect(self):
        return pygame.Rect(self.pos,self.dim)
    
    def sequence(self, surface, ticks):
        _ = False
        if self.pos[1] + self.dim[1] >= surface.get_rect().bottom:
            self.changeDirection()
            _ = True
        if not _: self.pull(self.gravity * ticks / 1000)
        self.move()
        surface.blit(self.surf, self.pos)

Og forritið:

#coding:utf8
import pygame, sys, os
from Boxes import AcceleratingBox
from pygame.locals import *
os.environ['SDL_VIDEO_WINDOW_POS'] = "50,50"
    
pygame.init()
screen = pygame.display.set_mode((1440,900))
#background = pygame.image.load("Guitar.jpg").convert()
clock = pygame.time.Clock()
boxes = []
boxes.append(AcceleratingBox((100,100),(25,25),(100,25,45)))
boxes.append(AcceleratingBox((200,150),(25,25),(45,25,100),8))
    
while True:
    timePassed = clock.tick()
    screen.fill((0,0,0))
    pygame.draw.aaline(screen,(255,255,255),(0,100),(1440,100),32)
    
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
    
    for box in boxes:
        box.sequence(screen,timePassed)
            
    pygame.display.flip()

Línan er til að sýna upphafsstaðsetningu rauða kassans
The cake is a Pie