SHOW:
|
|
- or go back to the newest paste.
| 1 | import pygame | |
| 2 | import os | |
| 3 | import math | |
| 4 | from random import randint as rand | |
| 5 | ||
| 6 | pygame.init() | |
| 7 | width, height = 970, 970 | |
| 8 | screen = pygame.display.set_mode((width, height)) | |
| 9 | h_center = ((height / 2) - 4) | |
| 10 | w_center = ((width / 2) - 4) | |
| 11 | ||
| 12 | speed = 2 # constant | |
| 13 | ||
| 14 | ||
| 15 | class Head(pygame.sprite.Sprite): | |
| 16 | def __init__(self): | |
| 17 | self.image = pygame.image.load('dotyellow.png')
| |
| 18 | self.x = (width / 2) | |
| 19 | self.y = (height / 2) | |
| 20 | self.speed = {'x': 0, 'y': 0}
| |
| 21 | self.deg = -90 # up, direction in degrees | |
| 22 | self.rect = self.image.get_rect() | |
| 23 | ||
| 24 | def handle_keys(self): | |
| 25 | key = pygame.key.get_pressed() | |
| 26 | dist = 1 | |
| 27 | if key[pygame.K_RIGHT]: | |
| 28 | self.deg += 2.3 | |
| 29 | elif key[pygame.K_LEFT]: | |
| 30 | self.deg -= 2.3 | |
| 31 | self.speed['x'] = speed * math.cos(math.radians(self.deg)) | |
| 32 | self.speed['y'] = speed * math.sin(math.radians(self.deg)) | |
| 33 | ||
| 34 | def move(self): | |
| 35 | self.y += self.speed['y'] | |
| 36 | self.x += self.speed['x'] | |
| 37 | # wrap to other side of screen | |
| 38 | if self.x > width - 13: | |
| 39 | self.x = 5 | |
| 40 | elif self.x < 0 + 5: | |
| 41 | self.x = width - 13 | |
| 42 | if self.y > height - 13: | |
| 43 | self.y = 5 | |
| 44 | elif self.y < 0 + 5: | |
| 45 | self.y = height - 13 | |
| 46 | ||
| 47 | def draw(self, surface): | |
| 48 | surface.blit(self.image, (self.x, self.y)) | |
| 49 | ||
| 50 | def is_collided_with(self, trail): | |
| 51 | return pygame.sprite.collide_circle(self, trail) | |
| 52 | ||
| 53 | ||
| 54 | class Trail(pygame.sprite.Sprite): | |
| 55 | def __init__(self): | |
| 56 | self.image = pygame.image.load('dotred.png')
| |
| 57 | self.segments = [None] * 100 # trail has 100 dots | |
| 58 | self.trailTrim = False # set True for constant trail length | |
| 59 | self.rect = self.image.get_rect() | |
| 60 | self.hitbox = self.segments | |
| 61 | ||
| 62 | ||
| 63 | def main(): | |
| 64 | head = Head() | |
| 65 | hole = 5 | |
| 66 | trail = Trail() | |
| 67 | clock = pygame.time.Clock() | |
| 68 | background = pygame.image.load('backgroundborder.png').convert()
| |
| 69 | running = True | |
| 70 | while running: | |
| 71 | for event in pygame.event.get(): | |
| 72 | if event.type == pygame.QUIT: | |
| 73 | pygame.quit() | |
| 74 | running = False | |
| 75 | if event.type == pygame.KEYDOWN: | |
| 76 | print("keydown")
| |
| 77 | ||
| 78 | head.handle_keys() | |
| 79 | head.move() | |
| 80 | screen.fill((200, 200, 200)) # clear screen | |
| 81 | screen.blit(background, (0, 0)) | |
| 82 | for d in trail.segments: | |
| 83 | if d: screen.blit(trail.image, d) | |
| 84 | if trail.trailTrim: | |
| 85 | del trail.segments[0] # delete trail end | |
| 86 | if hole >= 100 and rand(1,60) == 60: | |
| 87 | hole = 0 | |
| 88 | if hole >= 16: | |
| 89 | trail.segments.append((head.x, head.y)) # add current postiion | |
| 90 | head.draw(screen) # draw current point | |
| 91 | hole += 1 | |
| 92 | ||
| 93 | - | if head.is_collided_with(trail[:-10]): |
| 93 | + | if head.is_collided_with(trail): |
| 94 | print('collision!')
| |
| 95 | pygame.display.update() | |
| 96 | ||
| 97 | clock.tick(100) # 100 FPS | |
| 98 | ||
| 99 | ||
| 100 | if __name__ == '__main__': | |
| 101 | main() | |
| 102 |