Community
need help what to type to sync it with my start button
by: bluefire325, 8 years ago
import pygame
import time
BLACK = (0, 0, 0)
black = (0,0,0)
WHITE = (255, 255, 255)
BLUE = (50, 50, 255)
GREEN = (0,200,0)
RED = (200,0,0)
bright_green = (0,255,0)
bright_red = (255,0,0)
#kadakoa
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
gameDisplay = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
pygame.display.set_caption('Maze')
clock = pygame.time.Clock()
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
print(click)
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def quitgame():
pygame.quit()
quit()
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(WHITE)
basicfont = pygame.font.SysFont(None, 100)
text = basicfont.render('Funky Maze!', True, (100, 0, 0), (255, 255, 255))
textrect = text.get_rect()
textrect.centerx = screen.get_rect().centerx
textrect.centery = screen.get_rect().centery
screen.fill((255, 255, 255))
screen.blit(text, textrect)
center = ((SCREEN_WIDTH/2),(SCREEN_HEIGHT/2))
button("START",150,450,100,50,GREEN,bright_green,)
button("Quit",550,450,100,50,RED,bright_red,quitgame)
pygame.display.update()
clock.tick(15)
class Player(pygame.sprite.Sprite):
""" This class represents the bar at the bottom that the player
controls. """
# Constructor function
def __init__(self, x, y):
# Call the parent's constructor
super().__init__()
# ang gadagan
self.image = pygame.Surface([15, 15])
self.image.fill(WHITE)
# starting point
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
# speeed
self.change_x = 0
self.change_y = 0
self.walls = None
def update(self):
""" update the player pos. """
#left/right
self.rect.x += self.change_x
# para dli maglahos
block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
for block in block_hit_list:
if self.change_x > 0:
self.rect.right = block.rect.left
else:
self.rect.left = block.rect.right
# up/down
self.rect.y += self.change_y
# nakabanga
block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
for block in block_hit_list:
if self.change_y > 0:
self.rect.bottom = block.rect.top
else:
self.rect.top = block.rect.bottom
class Wall(pygame.sprite.Sprite):
""" Wall the player can run into. """
def __init__(self, x, y, width, height):
""" Constructor for the wall that the player can run into. """
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(BLUE)
#
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
pygame.init()
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption('Maze')
all_sprite_list = pygame.sprite.Group()
# Make the walls. (x_pos, y_pos, width, height)
wall_list = pygame.sprite.Group()
wall = Wall(0, 0, 10, 600)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(10, 0, 790, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(100,0,10,550)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(150,40,10,650)
wall_list.add(wall)
all_sprite_list.add(wall)
#1
wall = Wall(150,530,100,10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(200,60,10,400)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(200,60,100,10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(300,0,10,580)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(360,50,10,580)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(420,0,30,580)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(500,40,30,300)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(500,380,30,300)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(600,0,30,490)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(600,520,30,490)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(630, 200, 100, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(750, 40,10, 600)
wall_list.add(wall)
all_sprite_list.add(wall)
# player object
player = Player(50, 50)
player.walls = wall_list
all_sprite_list.add(player)
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changespeed(-3, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(3, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, -3)
elif event.key == pygame.K_DOWN:
player.changespeed(0, 3)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changespeed(3, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(-3, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, 3)
elif event.key == pygame.K_DOWN:
player.changespeed(0, -3)
all_sprite_list.update()
screen.fill(BLACK)
all_sprite_list.draw(screen)
pygame.display.flip()
clock.tick(60)
game_intro()
pygame.quit()