Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/main.py
blob: ed1ea30a5839df36f0cb84b693c49f0ac302312b (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Robot saving nature

import os
import sys
import time
import random

import pygame
from pygame.locals import *

from robot import Robot
from garbage import Garbage
from person import Person


pygame.init()

# Screen size
window_h = 600
window_w = 800
unit = 1.0

score = 0
robot_speed = 9
garbage_speed = 3
person_speed = 4


# Person generation interval
person_generation_ticks = 150
last_person_tick = 0
# Garbage generation interval
garbage_generation_ticks = 500
last_garbage_tick = 0


trash = []
people = []

screen = pygame.display.set_mode((window_w, window_h))

# Background image
object_background = pygame.image.load('images/park_2.png')


# Robot
robot = Robot(window_w, window_h, unit, robot_speed)
# Person
person = Person( window_w, window_h, unit, person_speed)

start_time = time.time()
fps = 30
frame_time = 1.0/fps

# Main loop
exit = False
lost_game = False
alert = 30

while not exit:
	
  # Process pygame events
	for event in pygame.event.get():
		# Escape key to quit the game
		if event.type == pygame.QUIT or \
			(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
			exit = True
			break
		robot.input(event)
	if exit:
		break

	current_time = time.time()
	if current_time - start_time <= frame_time:
		continue
	start_time = current_time
	
	if lost_game == False:
		# If enough time passed generate a new object
		if last_person_tick == 0:
			last_person_tick = person_generation_ticks
			people.append(Person( window_w, window_h, unit, person_speed))
		else:
			last_person_tick -= 1
			
	if lost_game == False:
		# If enough time passed generate a new object
		if last_garbage_tick == 0:
			last_garbage_tick = garbage_generation_ticks
			for person in people:
				trash.append(Garbage( window_w, window_h, unit, garbage_speed, person.position ))
		else:
			last_garbage_tick -= 1
  
	# Score text
	score_text = "Score: " + str(score) 
	font = pygame.font.Font(None, 36)
	text = font.render( score_text , 1, (0, 0, 0, 0))

    # check collisions
	for garbage in trash:
		if robot.collides_with(garbage):
			#lost_game = True
			trash.remove(garbage)
			score += 5
	
	# Remove objects that "died"
	for person in people:
		if person.died():
			people.remove(person)
	
	for garbage in trash:
		if garbage.died():
			trash.remove(garbage)
		
	# update objects
	for person in people:
		person.update()
	for garbage in trash:
		garbage.update()	
	
	robot.update()

	# draw the background first, because it will cover all the screen 	
	screen.blit(object_background, (0,0))
	# draw everything else over the background
	
	# Score
	screen.blit(text, (0, 0))
	
	# Persons
	for person in people:
		person.draw(screen)
	for garbage in trash:
		garbage.draw(screen)
	#Robot
	robot.draw(screen)
	
	# Alert when touching person
	for person in people:
		if robot.collides_with(person):
			robot.draw(screen)
			robot.draw_alert(screen)
			#score -= 1
	
	
	
	
	# Add score if you pick garbage
	#if position_garbage == position_robot:
	#	score = score + 1
	
	pygame.display.update()
  
pygame.quit()