Advertisement
Guest User

Pygame snake for android

a guest
Nov 26th, 2024
45
0
132 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.24 KB | Gaming | 0 0
  1. #pylint:disable=W0603
  2. import pygame
  3. import random
  4.  
  5.  
  6. COLOR_WHITE = (255,255,255)
  7. COLOR_RED = (255,0,0)
  8. COLOR_GREEN = (0,255,0)
  9. COLOR_BLUE = (0,0,255)
  10. COLOR_BLACK = (0,0,0)
  11. COLOR_MAGENTA = (255,0,255)
  12. SCREEN_W = 640
  13. SCREEN_H = 500
  14. ROOM_W = 900
  15. ROOM_H = 400
  16. VIEW_X = 0
  17. VIEW_Y = 0
  18. BORDER = 35
  19. ABUTTON_XPERC = 75
  20. ABUTTON_YPERC = 75
  21. ABUTTON_W = 20
  22. RIGHT = (1,0)
  23. LEFT = (-1,0)
  24. UP = (0,-1)
  25. DOWN = (0,1)
  26. SPEED = 16
  27.  
  28.  
  29.  
  30. EVENT_KEY_W = pygame.event.Event(pygame.KEYDOWN, unicode="w", key=pygame.K_w, mod=pygame.KMOD_NONE)
  31. #EVENT_KEY_A = pygame.event.Event(pygame.locals.KEYDOWN, unicode="a", key=pygame.locals.K_a, mod=pygame.locals.KMOD_NONE)
  32. #EVENT_KEY_S = pygame.event.Event(pygame.locals.KEYDOWN, unicode="s", key=pygame.locals.K_s, mod=pygame.locals.KMOD_NONE)
  33. #EVENT_KEY_D = pygame.event.Event(pygame.locals.KEYDOWN, unicode="d", key=pygame.locals.K_d, mod=pygame.locals.KMOD_NONE)
  34.  
  35. #pygame.event.post(EVENT_KEY_A)
  36.  
  37.  
  38. def draw_rect(surf,x,y,w,h,color):
  39.     rect = pygame.Rect(x,y,w,h)
  40.     pygame.draw.rect(surf,color,rect)
  41.  
  42. def percent(n):
  43.     return n * 0.01
  44.  
  45. def point_in_rect(x,y,rx,ry,rw,rh):
  46.     if x >= rx and y >= ry and x <= (rx+rw) and y <= (ry + rh):
  47.         return True
  48.     return False
  49.    
  50.  
  51. class Game:
  52.     def __init__(self):
  53.         self.running = False
  54.         self.mousedown = False
  55.         self.mouse_x = None
  56.         self.mouse_y = None
  57.         self.segments = []
  58.         self.buttons = []
  59.         self.device_w = 100
  60.         self.device_h = 400
  61.         self.next_dir = random.choice([UP,DOWN,LEFT,RIGHT])
  62.         pygame.init()
  63.         width = pygame.display.Info().current_w
  64.         height = pygame.display.Info().current_h
  65.         self.screen = pygame.display.set_mode([SCREEN_W-BORDER,SCREEN_H])
  66.         self.device_w, self.device_h = pygame.display.get_surface().get_size()
  67.         self.device_w = width
  68.         self.device_h = height
  69.         sx = random.randrange(BORDER,SCREEN_W)
  70.         sy = random.randrange(BORDER,SCREEN_H)
  71.         self.snakehead = Segment(sx,sy,self,0,COLOR_BLUE)
  72.         self.button_up = Button(self,100,600,60,60,COLOR_BLUE,COLOR_GREEN,EVENT_KEY_W)
  73.        
  74.        
  75.     def start(self):
  76.         self.running = True
  77.    
  78.     def main(self):
  79.         self.mouse_x = pygame.mouse.get_pos()[0]
  80.         self.mouse_y = pygame.mouse.get_pos()[1]
  81.         self.events()
  82.         self.draw()
  83.         clock.tick(fps)
  84.        
  85.     def events(self):
  86.         for event in pygame.event.get():
  87.             if event.type == pygame.QUIT:
  88.                 self.running = False
  89.             if event.type == pygame.MOUSEBUTTONDOWN:
  90.                 mpos = pygame.mouse.get_pos()
  91.                 self.mouse_x = mpos[0]
  92.                 self.mouse_y = mpos[1]
  93.             if event.type == pygame.KEYDOWN:
  94.                 if event.type == pygame.K_w:
  95.                     self.next_dir = UP
  96.                     self.button_up.current_color = COLOR_BLACK
  97.                        
  98.         for segment in self.segments:
  99.             segment.step()
  100.         for button in self.buttons:
  101.             button.check_click(self.mouse_x,self.mouse_y)
  102.    
  103.     def draw(self):
  104.         self.screen.fill(COLOR_BLACK)
  105.         draw_rect(game.screen,BORDER,BORDER,SCREEN_W,SCREEN_H,COLOR_WHITE)
  106.         for segment in self.segments:
  107.             segment.draw()
  108.         for button in self.buttons:
  109.             button.draw()
  110.         pygame.display.flip()  
  111.  
  112.  
  113. class Segment:
  114.     def __init__(self,x,y,context,index,color = COLOR_MAGENTA):
  115.         self.x = x
  116.         self.y = y
  117.         self.current_color = color
  118.         self.w = 16
  119.         self.h = 16
  120.         self.left = x
  121.         self.right = x+self.w
  122.         self.top = y
  123.         self.bottom = y+self.h
  124.         context.segments.append(self)
  125.         self.game = context
  126.         self.index = index
  127.    
  128.     def move(self,direction):
  129.         xoff = direction[0]
  130.         yoff = direction[1]
  131.         self.x = self.x + (xoff*SPEED)
  132.         self.y = self.y + (yoff*SPEED)
  133.    
  134.     def draw(self):
  135.         draw_rect(game.screen,self.x,self.y,16,16,self.current_color)
  136.    
  137.     def step(self):
  138.         self.move(game.next_dir)
  139.  
  140. class Button:
  141.     def __init__(self,context,x,y,w,h,color,color_click,event):
  142.         self.game = context
  143.         self.x = x
  144.         self.y = y
  145.         self.w = w
  146.         self.h = h
  147.         self.event = event
  148.         self.color = color
  149.         self.color_click = color_click
  150.         self.current_color = color
  151.         context.buttons.append(self)
  152.        
  153.     def check_click(self,x,y):
  154.         if point_in_rect(x,y,self.x,self.y,self.w,self.h):
  155.             self.onclick()
  156.            
  157.     def draw(self):
  158.         draw_rect(game.screen,self.x,self.y,self.w,self.h,self.current_color)
  159.            
  160.     def onclick(self):
  161.         self.current_color = self.color_click
  162.         pygame.event.post(self.event)
  163.  
  164. game = Game()
  165. clock = pygame.time.Clock()
  166. fps = 2
  167. game.start()
  168. while game.running:
  169.     game.main()
  170.    
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement