Pygame: Collision by Sides of Sprite

There is no function to get sides collision in PyGame. But you could try to use pygame.Rect.collidepoint to test if A.rect.midleft, A.rect.midright, A.rect.midtop, A.rect.midbottom, A.rect.topleft, A.rect.bottomleft , A.rect.topright, A.rect.bottomright are inside B.rect (pygame.Rect). EDIT: Example code. Use arrows to move player and touch enemy. (probably it is not optimal solution) import pygame WHITE = (255,255,255) … Read more

Can you style ordered list numbers?

You can do this using CSS counters, in conjunction with the :before pseudo element: ol { list-style: none; counter-reset: item; } li { counter-increment: item; margin-bottom: 5px; } li:before { margin-right: 10px; content: counter(item); background: lightblue; border-radius: 100%; color: white; width: 1.2em; text-align: center; display: inline-block; } <ol> <li>item</li> <li>item</li> <li>item</li> <li>item</li> </ol>

Changing colour of a surface without overwriting transparency

You can achieve this by 2 steps. self.original_image contains the filled rectangle with the desired color: self.original_image.fill(self.colour) Generate a completely white rectangle and transparent areas. Blend the rectangle with (self.image) and the blend mode BLEND_MAX (see pygame.Surface.blit): whiteTransparent = pg.Surface(self.image.get_size(), pg.SRCALPHA) whiteTransparent.fill((255, 255, 255, 0)) self.image.blit(whiteTransparent, (0, 0), special_flags=pg.BLEND_MAX) Now the rectangle is completely white, … Read more

How to turn the sprite in pygame while moving with the keys

See How do I rotate an image around its center using PyGame? for rotating a surface. If you want to rotate an image around a center point (cx, cy) you can just do that: rotated_car = pygame.transform.rotate(car, angle) window.blit(rotated_car, rotated_car.get_rect(center = (cx, cy)) Use pygame.math.Vector2 to store the position and the direction of movement. Change … Read more

How to move a sprite according to an angle in Pygame

you just need a little basic trig def calculat_new_xy(old_xy,speed,angle_in_radians): new_x = old_xy.X + (speed*math.cos(angle_in_radians)) new_y = old_xy.Y + (speed*math.sin(angle_in_radians)) return new_x, new_y — edit — Here is your code from above edited to work import pygame, math, time screen=pygame.display.set_mode((320,240)) clock=pygame.time.Clock() pygame.init() def calculate_new_xy(old_xy,speed,angle_in_radians): new_x = old_xy[0] + (speed*math.cos(angle_in_radians)) new_y = old_xy[1] + (speed*math.sin(angle_in_radians)) return new_x, … Read more

How to make sprite jump in java?

This is basic concept. Your implementation will change depending on the implementation of your engine. The basic idea is the player has a vertical delta which is changed over time by gravity. This effects the sprites vertical speed. This implementation also has a re-bound delta, which allows the sprite to re-bound rather the “stopping” suddenly. … Read more