Skip to content

Instantly share code, notes, and snippets.

@crazydiver
Last active February 12, 2020 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazydiver/db3d3e1b375fc3d773ebebfd254efc1a to your computer and use it in GitHub Desktop.
Save crazydiver/db3d3e1b375fc3d773ebebfd254efc1a to your computer and use it in GitHub Desktop.
Demo_ex-2019-5
import pygame as pg
FPS = 30
W = 1000
H = 1000
R = 50
menu = True
pg.init()
sc = pg.display.set_mode((W, H))
clock = pg.time.Clock()
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, r_p):
pg.sprite.Sprite.__init__(self)
self.__SPEED = 14
self.__r = r_p
self.__x = W // 2
self.__y = H // 2
# self.player_s = pg.Surface((self.__r * 2, self.__r * 2))
def draw(self, coords=(W // 2, H // 2)):
self.pic = pg.draw.circle(sc, (255, 0, 0), (self.__x, self.__y), self.__r)
def move(self, direction):
if direction == 'up' and self.__y - self.__r - 3 > 0:
self.__y -= self.__SPEED
elif direction == 'down' and self.__y + self.__r + 3 < H:
self.__y += self.__SPEED
elif direction == 'left' and self.__x - self.__r - 3 > 0:
self.__x -= self.__SPEED
elif direction == 'right' and self.__x + self.__r + 3 < W:
self.__x += self.__SPEED
def stop():
exit()
def centre_button():
global menu
menu = False
def main():
buttons = []
buttons.append(Button(20, W - 60, 100, 50, stop, 'EXIT'))
buttons.append(Button(W // 2 - 130, H // 2 - 50, 130, 50, centre_button, 'START', centred=True))
player = Player(R)
while True:
clock.tick(FPS)
events = pg.event.get()
sc.fill((255, 255, 255))
if menu:
for button in buttons:
button.draw(sc)
else:
buttons[0].draw(sc)
player.draw()
for event in events:
if event.type == pg.QUIT:
return
elif event.type == pg.MOUSEBUTTONDOWN:
for button in buttons:
button.on_mouse_down(*event.pos)
elif event.type == pg.MOUSEBUTTONUP:
for button in buttons:
button.on_mouse_up(*event.pos)
elif event.type == pg.MOUSEMOTION:
for button in buttons:
button.on_mouse_move(*event.pos)
keys = pg.key.get_pressed()
if keys[pg.K_w]:
player.move('up')
elif keys[pg.K_s]:
player.move('down')
elif keys[pg.K_a]:
player.move('left')
elif keys[pg.K_d]:
player.move('right')
pg.display.update()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment