Tentador. Concurso de minigames hecho con el ge, ya.Vaya, pequeño saltamontes, yo puedo hacer un juego cuando quiera y sin salir de Blender y el que no lo crea, que me rete si tiene webs.
Tentador. Concurso de minigames hecho con el ge, ya.Vaya, pequeño saltamontes, yo puedo hacer un juego cuando quiera y sin salir de Blender y el que no lo crea, que me rete si tiene webs.
Hola, ahora me estoy intentando centrar en los rect, para quien no lo sepa es un module de Pygame a partir del cual se crean rectángulos inmaginarios para detectar colisiones.
Bueno, a partir de la siguiente función que me venía en la página oficial de Pygame:
Pygame. Rect(left, top, width, height): return rect.
Yo escribí lo siguiente en el código:
Self, rect = Pygame. Rect(235, 775, 50, 50).
Pensando que left y top son las coordinadas del centro y width y height son el alto y el ancho del rectángulo, es decir, yo me imagino que será algo así:
_______________________.
| |.
| |.
| |.
| |.
|.(centro) |(height).
| |.
| |.
| |.
|_______________________|.
(Width).
Si estuviera en lo cierto, me debería funcionar ya las colisiones, pero no es así, ni tampoco me sale un error, porlocual deben estar mal colocadas, alguien sabe cómo posicionar bien los rects?
Left y top son las coordenadas de la esquina superior izquierda (top left).
No ha quedado bien el cubo, no sé porque, pero se pilla la idea, ¿no?
La esquina superior izquierda, de la pantalla?
Y cómo se sitúa el rectángulo donde tú quieres? Usando move _ip?
Perdón, entendí mal tu respuesta. Aquí subo el código que me gustaría saber porqué r no ocurre nada al chocar mi nave contra un obstáculo, lo publicaré en algún otro sitio también:
Import Pygame.
From Pygame, locals import *.
Class prota (Pygame, sprite. Sprite):
Def __init__(self):
Pygame, sprite. Sprite.__init__(self).
Self, rect = Pygame. Rect(25, 25, 50, 50).
Self, image = Pygame, image, load(nave, png).
Self, height = 50.
Self.width = 50.
Self.x = 210.
Self, y = 750.
Self, rect = Pygame. Rect(self.x, self y, 50, 50).
Self.key = Pygame.key, get_pressed().
Def update (self):
Self.key = Pygame.key, get_pressed().
If self.key[k_right] and self.x < 370:
Self.x += 6.
If self.key[k_left] and self.x > 0:
Self.x -= 6.
Self, y -= 5.
If self, y <= -50:
Self, y = 850.
W.dif += 1.
Def draw(self):
Self, rect, move_ip(self.x, self, y).
W.screen, Blit(self, image, (self.x, self, y)).
Class obstacle (Pygame, sprite. Sprite):
Def __init__(self, px, py):
Pygame, sprite. Sprite.__init__(self).
Self, rect = Pygame. Rect(px, py, 50, 25).
Self, state = 1.
Self.x = px.
Self, y = py.
Self, image = Pygame, image, load(obstáculos, png).
Def update (self, velocidad):
If self.x >= 370:self, state = 2.
Elif self.x <= 0:self, state = 1.
If self, state == 1:
Self.x += velocidad.
Elif self, state == 2:
Self.x -= velocidad.
Def draw(self, screen):
Self, rect, move_ip(self.x, self, y).
Scren, Blit(self, image, (self.x, self, y)).
Class world:
Def __init__(self):
Pygame, init().
Self, black = (100, 200, 100).
Self, scren_size = (420, 800).
Self, screen = Pygame, display, set_mode (self, scren_size).
Pygame, display, set_caption(game nave).
Self, fondo = Pygame, image, load(fondo, png).
Self, kit = false.
Self, dif = 0.
Self, clok = Pygame, time. Clock().
Self, nave = prota ().
Self, obst1 = obstacle (0, 140).
Self, obst2 = obstacle (105, 280).
Self, obst3 = obstacle (210, 420).
Self, obst4 = obstacle (315, 560).
Self, obst5 = obstacle (0, 680).
Self, obst6 = obstacle (300, 140).
Self, obst7 = obstacle (405, 280).
Self, obst8 = obstacle (10, 420).
Self, obst9 = obstacle (15, 560).
Self, obst0 = obstacle (300, 680).
Self, obst_group = Pygame, sprite. Group().
Self, obst_group, add(self, obst1, self, obst2, self, obst3, self, obst4, self, obst5, self, obst6, self, obst7, self, obst8, self, obst9, self, obst0).
Def update (self):
Self, nave, update ().
Self, obst1.update (9 + self, dif).
Self, obst2.update (8 + self, dif).
Self, obst3.update (7 + self, dif).
Self, obst4.update (6 + self, dif).
Self, obst5.update (5 + self, dif).
Self, obst6.update (4 + self, dif).
Self, obst7.update (5 + self, dif).
Self, obst8.update (6 + self, dif).
Self, obst9.update (7 + self, dif).
Self, obst0.update (8 + self, dif).
Def draw(self):
Self, screen, fill(self, black).
Self, screen, Blit(self, fondo, (0, 0)).
Self, obst1.draw(self, screen).
Self, obst2.draw(self, screen).
Self, obst3.draw(self, screen).
Self, obst4.draw(self, screen).
Self, obst5.draw(self, screen).
Self, obst6.draw(self, screen).
Self, obst7.draw(self, screen).
Self, obst8.draw(self, screen).
Self, obst9.draw(self, screen).
Self, obst0.draw(self, screen).
Self, nave, draw().
Pygame, display, flip().
Def game (self):
While not self, quit:
For event in Pygame, event, get():
If event, type == quit:
Self, kit = true.
If w.nave.key[k_escape]:
Self, kit = true.
If Pygame, sprite, spritecollideany(self, nave, self, obst_group) == true:
Self, kit = true.
Self, update ().
Self, draw().
Self, clock, tick(60).
If __name__ == __main__:
W = world().
W.game ().