Open ArbMehmeti opened 6 years ago
""" Created on Fri Apr 27 16:48:54 2018
@author: ASUS STRIX """
import pygame
class Ball: def init(self,size,x,y,xvel,height): self.size = size self.x = x self.y = y self.xvel = xvel self.yvel = 20 self.height = height
def update(self): self.x += self.xvel if self.x >= 720: self.xvel = -abs(self.xvel) elif self.x <= 0: self.xvel = abs(self.xvel) self.y += self.yvel self.yvel -= gravity if (self.y < self.height): self.yvel += gravity self.yvel *= -1 elif (self.y > 480): self.yvel *= -1 def display(self, surface): pygame.draw.circle(surface, (0,200,0),(self.x,self.y) ,self.size) def split(self): ball1 = Ball(self.size/2, self.x, self.y, self.xvel, self.height/2) ball2 = Ball(self.size/2, self.x, self.y, -self.xvel, self.height/2) return [ball1, ball2]
class Arrow:#(pygame.sprite.Sprite):
def __init__(self, attribute, trail, velocity, position): self.attribute = attribute self.image = pygame.image.load('arrow.png') self.velocity = 10 #self.trail = trail self.position = 480
#self.color = pygame.Color.r def shoot(self): self.position-=self.velocity def display(self, surface,player_position): # pygame.draw.line((surface, trim, (266, 257), (266, 162), 2)) arrowrect = pygame.Rect(0,0,8, 480 - self.position) surface.blit(self.image, (player_position, self.position), arrowrect)
class Player():
def __init__(self, surface): self.surface = surface self.img = pygame.image.load('player.png').convert_alpha() width = self.img.get_width() self.box = pygame.Rect(round(self.surface.get_width() / 2 - width / 2), round(self.surface.get_height() * 0.9), width, self.img.get_height()) def display(self): self.surface.blit(self.img, self.box.topleft) def left(self): if self.box.left - 10 > 0: self.box.move_ip(-10, 0) def right(self): if self.box.right + 10 < self.surface.get_width(): self.box.move_ip(10, 0) def current_position(self): return self.box.x #player-ball interaction #player arrow interaction
pygame.init() gravity = 5 surface = pygame.display.set_mode((720, 480)) player = Player(surface) arrow = Arrow(100, 50, 4, 199) first = Ball(10, 1, 360, 10, 315) pygame.display.set_caption('ballbounce')
play = True while play: for event in pygame.event.get(): if event.type == pygame.QUIT: play = False break
first.display(surface) first.update() player.display() sp=pygame.key.get_pressed() player_position= player.current_position() if sp[pygame.K_SPACE]: if arrow.position!=0: arrow.display(surface,player_position) arrow.shoot() if arrow.position==0: arrow.position=480 pygame.display.flip() pygame.time.wait(int(1/15 * 1000)) keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player.left() if keys[pygame.K_RIGHT]: player.right() surface.fill((0, 0, 0)) pygame.display.update()
pygame.display.quit()
-- coding: utf-8 --
""" Created on Fri Apr 27 16:48:54 2018
@author: ASUS STRIX """
import pygame
class Ball: def init(self,size,x,y,xvel,height): self.size = size self.x = x self.y = y self.xvel = xvel self.yvel = 20 self.height = height
class Arrow:#(pygame.sprite.Sprite):
self.player=240
print(arrow.image.get_rect())
class Player():
pygame.init() gravity = 5 surface = pygame.display.set_mode((720, 480)) player = Player(surface) arrow = Arrow(100, 50, 4, 199) first = Ball(10, 1, 360, 10, 315) pygame.display.set_caption('ballbounce')
play = True while play: for event in pygame.event.get(): if event.type == pygame.QUIT: play = False break
pygame.display.quit()
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.left()
if event.key == pygame.K_RIGHT:
player.right()