Created
February 23, 2020 19:05
-
-
Save crazydiver/096527c8b37ab34ac0fda219a945ec4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame as pg | |
import random | |
FPS = 30 | |
W = 1000 | |
H = 1000 | |
R = 50 | |
menu = True | |
pg.init() | |
sc = pg.display.set_mode((W, H)) | |
clock = pg.time.Clock() | |
random.seed() | |
class Button: | |
def __init__(self, x, y, w, h, func, words, centred=False): | |
self.button_s = pg.Surface((w, h)) | |
if centred: | |
self.rect = pg.Rect(x + w // 2, y + h // 2, w, h) | |
else: | |
self.rect = pg.Rect(x, y, w, h) | |
f = pg.font.Font(None, 35) | |
self.text = f.render(words, 1, (0, 0, 0)) | |
self.place = self.text.get_rect(center=(w // 2, h // 2)) | |
aaa = 6 | |
# self.button_s.blit(text, (place[0], place[1])) | |
self.action = func # what to do if click completed | |
self.clicked = False | |
self.last_click_location = (0, 0) | |
self.hovered = False | |
hover = (111, 134, 168) | |
pushed = (204, 210, 219) | |
stand = (219, 219, 219) | |
def draw(self, parent_lay): | |
if self.clicked: | |
self.button_s.fill(self.pushed) | |
else: | |
self.button_s.fill(self.hover if self.hovered else self.stand) | |
self.button_s.blit(self.text, (self.place[0], self.place[1])) | |
parent_lay.blit(self.button_s, self.rect) | |
def on_mouse_down(self, x, y): | |
if self.rect.collidepoint(x, y): | |
self.clicked = True | |
self.last_click_location = x, y | |
else: | |
self.clicked = False | |
def on_mouse_up(self, x, y): | |
if (x, y) == self.last_click_location: | |
self.action() | |
self.clicked = False | |
def on_mouse_move(self, x, y): | |
self.hovered = self.rect.collidepoint(x, y) | |
class Player(pg.sprite.Sprite): | |
def __init__(self): | |
pg.sprite.Sprite.__init__(self) | |
self.__color = (255, 82, 82) | |
self.x = 60 | |
self.y = H // 2 | |
self.__SPEED = 10 | |
def draw(self): | |
coords = [self.x, self.y] | |
self.pic = pg.draw.polygon(sc, self.color, [coords, [coords[0]-50, coords[1] - 20], [coords[0] - 30, coords[1]], [coords[0] - 50, coords[1] + 20]]) | |
def move(self, dir): | |
if dir == 'up' and self.y - 20 > 0: | |
self.y -= self.__SPEED | |
elif dir == 'down' and self.y + 20 < H: | |
self.y += self.__SPEED | |
class Bubble: | |
def __init__(self): | |
self.__color = (0, 0, 0) | |
self.__coords = (random.randint(100, W), random.randint(0, H)) | |
self.__r = random.randint(5, 50) | |
def draw(self): | |
self.__pic = pg.draw.circle(sc, self.__color, self.__coords, self.__r, 2) | |
def collision(self): | |
if self.__coords[0] - self.__r <= player.x <= | |
def stop(): | |
exit() | |
def centre_button(): | |
global menu | |
menu = False | |
def main(): | |
menu_buttons = [] | |
menu_buttons.append(Button(20, W - 60, 100, 50, stop, 'EXIT')) | |
menu_buttons.append(Button(W // 2 - 130, H // 2 - 50, 130, 50, centre_button, 'START', centred=True)) | |
player = Player() | |
global menu | |
while True: | |
clock.tick(FPS) | |
events = pg.event.get() | |
sc.fill((62, 84, 125)) | |
if menu: | |
for button in menu_buttons: | |
button.draw(sc) | |
else: | |
# menu_buttons[0].draw(sc) | |
player.draw() | |
for event in events: | |
if event.type == pg.QUIT: | |
return | |
elif event.type == pg.MOUSEBUTTONDOWN: | |
for button in menu_buttons: | |
button.on_mouse_down(*event.pos) | |
elif event.type == pg.MOUSEBUTTONUP: | |
for button in menu_buttons: | |
button.on_mouse_up(*event.pos) | |
elif event.type == pg.MOUSEMOTION: | |
for button in menu_buttons: | |
button.on_mouse_move(*event.pos) | |
elif event.type == pg.KEYUP: | |
if event.key == pg.K_ESCAPE: | |
menu = not menu | |
keys = pg.key.get_pressed() | |
if keys[pg.K_s]: | |
player.move('down') | |
elif keys[pg.K_w]: | |
player.move('up') | |
pg.display.update() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment