Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pylint:disable=W0603
- import pygame
- import random
- COLOR_WHITE = (255,255,255)
- COLOR_RED = (255,0,0)
- COLOR_GREEN = (0,255,0)
- COLOR_BLUE = (0,0,255)
- COLOR_BLACK = (0,0,0)
- COLOR_MAGENTA = (255,0,255)
- SCREEN_W = 640
- SCREEN_H = 500
- ROOM_W = 900
- ROOM_H = 400
- VIEW_X = 0
- VIEW_Y = 0
- BORDER = 35
- ABUTTON_XPERC = 75
- ABUTTON_YPERC = 75
- ABUTTON_W = 20
- RIGHT = (1,0)
- LEFT = (-1,0)
- UP = (0,-1)
- DOWN = (0,1)
- SPEED = 16
- EVENT_KEY_W = pygame.event.Event(pygame.KEYDOWN, unicode="w", key=pygame.K_w, mod=pygame.KMOD_NONE)
- #EVENT_KEY_A = pygame.event.Event(pygame.locals.KEYDOWN, unicode="a", key=pygame.locals.K_a, mod=pygame.locals.KMOD_NONE)
- #EVENT_KEY_S = pygame.event.Event(pygame.locals.KEYDOWN, unicode="s", key=pygame.locals.K_s, mod=pygame.locals.KMOD_NONE)
- #EVENT_KEY_D = pygame.event.Event(pygame.locals.KEYDOWN, unicode="d", key=pygame.locals.K_d, mod=pygame.locals.KMOD_NONE)
- #pygame.event.post(EVENT_KEY_A)
- def draw_rect(surf,x,y,w,h,color):
- rect = pygame.Rect(x,y,w,h)
- pygame.draw.rect(surf,color,rect)
- def percent(n):
- return n * 0.01
- def point_in_rect(x,y,rx,ry,rw,rh):
- if x >= rx and y >= ry and x <= (rx+rw) and y <= (ry + rh):
- return True
- return False
- class Game:
- def __init__(self):
- self.running = False
- self.mousedown = False
- self.mouse_x = None
- self.mouse_y = None
- self.segments = []
- self.buttons = []
- self.device_w = 100
- self.device_h = 400
- self.next_dir = random.choice([UP,DOWN,LEFT,RIGHT])
- pygame.init()
- width = pygame.display.Info().current_w
- height = pygame.display.Info().current_h
- self.screen = pygame.display.set_mode([SCREEN_W-BORDER,SCREEN_H])
- self.device_w, self.device_h = pygame.display.get_surface().get_size()
- self.device_w = width
- self.device_h = height
- sx = random.randrange(BORDER,SCREEN_W)
- sy = random.randrange(BORDER,SCREEN_H)
- self.snakehead = Segment(sx,sy,self,0,COLOR_BLUE)
- self.button_up = Button(self,100,600,60,60,COLOR_BLUE,COLOR_GREEN,EVENT_KEY_W)
- def start(self):
- self.running = True
- def main(self):
- self.mouse_x = pygame.mouse.get_pos()[0]
- self.mouse_y = pygame.mouse.get_pos()[1]
- self.events()
- self.draw()
- clock.tick(fps)
- def events(self):
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- self.running = False
- if event.type == pygame.MOUSEBUTTONDOWN:
- mpos = pygame.mouse.get_pos()
- self.mouse_x = mpos[0]
- self.mouse_y = mpos[1]
- if event.type == pygame.KEYDOWN:
- if event.type == pygame.K_w:
- self.next_dir = UP
- self.button_up.current_color = COLOR_BLACK
- for segment in self.segments:
- segment.step()
- for button in self.buttons:
- button.check_click(self.mouse_x,self.mouse_y)
- def draw(self):
- self.screen.fill(COLOR_BLACK)
- draw_rect(game.screen,BORDER,BORDER,SCREEN_W,SCREEN_H,COLOR_WHITE)
- for segment in self.segments:
- segment.draw()
- for button in self.buttons:
- button.draw()
- pygame.display.flip()
- class Segment:
- def __init__(self,x,y,context,index,color = COLOR_MAGENTA):
- self.x = x
- self.y = y
- self.current_color = color
- self.w = 16
- self.h = 16
- self.left = x
- self.right = x+self.w
- self.top = y
- self.bottom = y+self.h
- context.segments.append(self)
- self.game = context
- self.index = index
- def move(self,direction):
- xoff = direction[0]
- yoff = direction[1]
- self.x = self.x + (xoff*SPEED)
- self.y = self.y + (yoff*SPEED)
- def draw(self):
- draw_rect(game.screen,self.x,self.y,16,16,self.current_color)
- def step(self):
- self.move(game.next_dir)
- class Button:
- def __init__(self,context,x,y,w,h,color,color_click,event):
- self.game = context
- self.x = x
- self.y = y
- self.w = w
- self.h = h
- self.event = event
- self.color = color
- self.color_click = color_click
- self.current_color = color
- context.buttons.append(self)
- def check_click(self,x,y):
- if point_in_rect(x,y,self.x,self.y,self.w,self.h):
- self.onclick()
- def draw(self):
- draw_rect(game.screen,self.x,self.y,self.w,self.h,self.current_color)
- def onclick(self):
- self.current_color = self.color_click
- pygame.event.post(self.event)
- game = Game()
- clock = pygame.time.Clock()
- fps = 2
- game.start()
- while game.running:
- game.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement