blob: 15938b172f36ec4b6a9ff4f401b2182f97783631 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
import pygame
class Robot:
def __init__(self, screen_w, screen_h, unit, speed):
self.unit = unit
self.speed = speed
self.image = pygame.image.load("images/robot_1.png")
image_rect = self.image.get_rect()
self.position = [0, 0]
self.screen_w = screen_w
self.screen_h = screen_h
self.direction_x = 0
self.direction_y = 0
def input(self, event):
# Handle movement for the character
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.direction_x = -1
self.direction_y = 0
if event.key == pygame.K_RIGHT:
self.direction_x = 1
self.direction_y = 0
if event.key == pygame.K_UP:
self.direction_y = -1
self.direction_x = 0
if event.key == pygame.K_DOWN:
self.direction_y = 1
self.direction_x = 0
def update(self):
image_center = self.image.get_width()/2
# Move the character around the screen
self.position = ((self.position[0] + self.direction_x * self.speed * self.unit + image_center)%self.screen_w - image_center), \
((self.position[1] + self.direction_y * self.speed * self.unit + image_center)%self.screen_h - image_center)
def draw(self, on_surface):
on_surface.blit(self.image, self.position)
|