Форум сайта python.su
Вообщем была такая игра “парашют”. Заключалась она в том, что в центре экрана располагалась башня ,которая могла вращаться от 0 до 180 градусов, а сверху сбрасывался десант который предстояло отстреливать. Собственно такое задание мне задали на курсовую. Уже неделю убил на эту хрень, а прогресса все нет. Хотел спросить о помощи.
Задача заключается в том, что в окне 640х480 должны быть расположены две башни по бокам на оси y = y/2, которые могут двигать стволами орудий соответственно от 270 до 90 градусов и наоборот, а десант сбрасывался строго по центру в разном кол-ве не больше 5 и пересекая линию, где у = 320. Нанося при этом базе урон.
Звучало легко, а сделать что-то не получается.
Мое понимание:
Используя:
сlass CoordSysConverter:
def __init__(self, center):
self.center_x, self.center_y = center
# pygame -> polar
def pygame2polar(self, x, y):
rx, ry = self._pygame2cartesian(x, y)
return self._cartesian2polar(rx, ry)
# polar -> pygame
def polar2pygame(self, r, fi):
x, y = self._polar2cartesian(r, fi)
return self._cartesian2pygame(x, y)
# pygame -> cartesian
def _pygame2cartesian(self, x, y):
rx = x - self.center_x
ry = self.center_y - y
return rx, ry
# cartesian -> pygame
def _cartesian2pygame(self, x, y):
rx = x + self.center_x
ry = self.center_y - y
return rx, ry
# polar -> cartesian
def _polar2cartesian(self, r, fi):
x = r * math.cos(fi)
y = r * math.sin(fi)
return x, y
# cartesian -> polar
def _cartesian2polar(self, x, y):
r = math.sqrt(x**2 + y**2)
if r == 0:
return r, 0
if x == 0:
if y > 0:
return r, math.pi / 2
else:
return r, math.pi + math.pi / 2
fi = math.atan(y / x)
if x < 0:
fi += math.pi
if fi < 0:
fi += math.pi * 2
return r, fi
import pygame
import time
import math
pygame.init()
#main def
def main():
#screen
size = 640, 480
screen = pygame.display.set_mode(size, pygame.DOUBLEBUF | pygame.HWSURFACE)
pygame.display.set_caption('Landing')
#process event
damage = False
done = False
a = 2
turretl = Turretl(size, a)
turretr = Turretr(size, a)
troop = Troop(size)
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
turretl.events(event)
turretr.events(event)
#step
turretl.step()
turretr.step()
damage = turretl.step_bullets(troop) or turretr.step_ballets(troop)
if troop_dead():
a = a + 1
#rendering
if damage:
screen.fill((255, 0, 0))
damage = True
else:
screen.fill((255, 255, 255))
turretl.render(screen)
for bulletl in turretl.bullets:
bulletl.render(screen)
turretr.render(screen)
for bulletr in turretr.ballets:
bulletr.render(screen)
pygame.draw.line(screen, (255, 0, 0), (0, 340), (640, 340), 3)
pygame.display.flip()
#wait
time.sleep(0.20)
#Classes
class Troop:
def __init__(self, screen_size):
self.x = 320
self.y = 0
self.dy = 2
self.r = 10
self.screen_size = screen_size
self.pain = 0
def step(self):
self.y -= self.dy
def cross_line(self):
if self.y == 440:
return True
return False
def render(self, screen):
pygame.draw.circle(screen, (0, 50, 0), (self.x, self.y), self.r)
def troop_dead(self):
if self.pain <= 2:
self.pain += 1
def pain(self):
if self.pain >= 2.5:
return True
return False
class Turretl:
def __init__(self, screen_size, a):
self.x = 0
self.y = 320
self.damage = 0
self.screen_size = screen_size
self.bullets = []
self.r = 50
self.a = a
def events(self, event):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.y += 15
if event.key == pygame.K_DOWN:
self.y -= 15
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and self.i == 2:
bulletl = Bulletl(self.screen_size, (self.x, self.y))
self.bullets.append(bulletl)
self.a -= 1
def step_bullets(self, troop):
damage = False
for i in reversed(range(0, len(self.bullets))):
self.bullets[i].step()
if self.bullets[i].is_out_of_screen():
self.bullets.pop(i)
elif self.bullets[i].in_troop(troop):
damage = True
self.bullets.pop(i)
troop.pain()
return damage
def step(self):
if self.damage > 0:
self.damage += 0.00001
def render(self, screen):
pygame.draw.circle(screen, (255, 40, 0), (self.x, self.y), self.r)
rect = pygame.Rect((self.x - 10, self.y - 10), (20, 20))
pygame.draw.rect(screen, (100, 100, 40), rect)
def damage(self):
if self.damage <= 10:
self.damage += 1
def death(self):
if self.damage > 10.5:
return True
return False
class Turretr:
def __init__(self, screen_size, a):
self.x = 480
self.y = 320
self.damage = 0
self.screen_size = screen_size
self.ballets = []
self.r = 50
self.a = a
def events(self, event):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_W:
self.y += 15
if event.key == pygame.K_S:
self.y -= 15
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_P and self.a == 2:
bulletr = Bulletr(self.screen_size, (self.x, self.y))
self.ballets.append(bulletr)
self.a -= 1
def step(self):
if self.damage > 0:
self.damage += 0.00001
def render(self, screen):
pygame.draw.circle(screen, (255, 40, 0), (self.x, self.y), self.r)
rect = pygame.Rect((self.x - 10, self.y - 10), (20, 20))
pygame.draw.rect(screen, (100, 100, 40), rect)
def damage(self):
if self.damage <= 10:
self.damage += 1
def death(self):
if self.damage > 10.5:
return True
return False
def step_ballets(self, troop):
damage = False
for i in reversed(range(0, len(self.ballets))):
self.ballets[i].step()
if self.ballets[i].is_out_of_screen():
self.ballets.pop(i)
elif self.ballets[i].in_troop(troop):
damage = True
self.ballets.pop(i)
troop.pain()
return damage
class Bulletl:
def __init__(self, screen, pos):
self.x, self.y = pos
self.dx = 5
self.r = 4
self.screen_size = screen_size
def step(self):
self.x -= self.dx
def in_troop(self, troop):
if self.x + self.r > troop.x:
if self.y + self.r > troop.y and self.y - self.r < troop.y + troop.r:
return True
return False
def is_out_of_screen(self):
if (self.y - self.r) > self.screen_size[1]:
return True
return False
def in_turret(self, turretr):
if self.x + self.r > turretr.x:
if self.y + self.r > turretr.y and self.y - self.r < turretr.y + turretr.r:
return True
return False
def render(self, screen):
pygame.draw.circle(screen, (8, 80, 80), (self.x, self.y), self.r)
class Bulletr:
def __init__(self, screen, pos):
self.x, self.y = pos
self.dx = 5
self.r = 4
self.screen_size = screen_size
def step(self):
self.x += self.dx
def is_out_of_screen(self):
if (self.y - self.r) > self.screen_size[1]:
return True
return False
def in_turret(self, turretl):
if self.x + self.r > turretl.x:
if self.y + self.r > turretl.y and self.y - self.r < turretl.y + turretl.r:
return True
return False
def in_troop(self, troop):
if self.x + self.r > troop.x:
if self.y + self.r > troop.y and self.y - self.r < troop.y + troop.r:
return True
return False
def render(self, screen):
pygame.draw.circle(screen, (8, 80, 80), (self.x, self.y), self.r)
#class Land:
#def
#class Base:
#def __init__(self, screen_size):
#class Supertroop:
#def __init__():
class CoordSysConverter:
def __init__(self, center):
self.center_x, self.center_y = center
# pygame -> polar
def pygame2polar(self, x, y):
rx, ry = self._pygame2cartesian(x, y)
return self._cartesian2polar(rx, ry)
# polar -> pygame
def polar2pygame(self, r, fi):
x, y = self._polar2cartesian(r, fi)
return self._cartesian2pygame(x, y)
# pygame -> cartesian
def _pygame2cartesian(self, x, y):
rx = x - self.center_x
ry = self.center_y - y
return rx, ry
# cartesian -> pygame
def _cartesian2pygame(self, x, y):
rx = x + self.center_x
ry = self.center_y - y
return rx, ry
# polar -> cartesian
def _polar2cartesian(self, r, fi):
x = r * math.cos(fi)
y = r * math.sin(fi)
return x, y
# cartesian -> polar
def _cartesian2polar(self, x, y):
r = math.sqrt(x**2 + y**2)
if r == 0:
return r, 0
if x == 0:
if y > 0:
return r, math.pi / 2
else:
return r, math.pi + math.pi / 2
fi = math.atan(y / x)
if x < 0:
fi += math.pi
if fi < 0:
fi += math.pi * 2
return r, fi
class Ttower:
def __init__(self, csc, length, width, r):
self.csc = csc
self.length = 2
self.width = 1
self.fi = math.pi / 12
def set_fi(self, fi):
self.fi = fi
#def process_event(self, event):
#if event.type == pygame.KEYDOWN:
#if event.key == pygame.K_LEFT:
def render(self, screen):
center = self.csc.center_x, self.csc.center_y
end = self.csc.polar2pygame(self.length, self.fi)
center = int(center[0]), int(center[1])
end = int(end[0]), int(end[1])
pygame.draw.line(screen, (0, 0, 90), center, end, self.width)
main()
Отредактировано (Ноя. 30, 2011 07:49:39)
Офлайн