Pygame Fighter - Attacking Tweaks

  Рет қаралды 475

HammerHeadGameStudio

HammerHeadGameStudio

2 жыл бұрын

Thanks again to Coding with Ross.
I had issues embedding the code in comments initially because of length. See GitHub repo below instead.
github.com/charlieleeCoder/Cl...

Пікірлер: 6
@dormii9409
@dormii9409 Жыл бұрын
Thank you so much your a genius i was really struggling i tried so much things then i found this video
@dormii9409
@dormii9409 Жыл бұрын
liked and subbed for that
@Asmar76if
@Asmar76if Жыл бұрын
❤️
@HammerHeadGameStudio
@HammerHeadGameStudio 2 жыл бұрын
these are in reverse order btw...
@HammerHeadGameStudio
@HammerHeadGameStudio 2 жыл бұрын
# physics # apply gravity self.vel_y += GRAVITY dy += self.vel_y # ensure player stays on screen if self.rect.left + dx screen_width: dx = screen_width - self.rect.right if self.rect.bottom + dy > screen_height - 110: self.vel_y = 0 self.jump = False dy = screen_height - 110 - self.rect.bottom #ensure players face each other if target.rect.centerx > self.rect.centerx: self.flip = False else: self.flip = True #apply attack cooldown if self.attack_cooldown > 0: self.attack_cooldown -= 1 # update player position self.rect.x += dx self.rect.y += dy if self.player == 1: if key[pygame.K_a] == False: if key[pygame.K_d] == False: self.running = False # reset self.running if self.player == 2: if key[pygame.K_LEFT] == False: if key[pygame.K_RIGHT] == False: self.running = False # reset self.running # handle animation updates (from tutorial) def update(self): animation_cooldown = 100 self.image = self.animation_list[self.action][self.frame_index] # check what action the player is performing if self.hit == True: self.update_action(2) # getting hit # elif self.attacking == True: # if self.attack_type == 1: # self.update_action(3) # 3:attack 1 # elif self.attack_type == 2: # self.update_action(4) # 4:attack 2 # elif self.attack_type == 3: # self.update_action(5) # 5:attack 3 # elif self.attack_type == 4: # self.update_action(6) # 6:attack 4 # else: # self.update_action(7) # shoot attack # elif self.jump == True: # self.update_action(2) # 2:jump elif self.running == True: self.update_action(1) # 1:run else: self.update_action(0) # 0:idle # check if enough time has passed since the last update if pygame.time.get_ticks() - self.update_time > animation_cooldown: self.frame_index += 1 self.update_time = pygame.time.get_ticks() # check if the animation has finished if self.frame_index >= len(self.animation_list[self.action]): # if the player is dead then end the animation_list # if self.alive == False: #self.frame_index = len(self.animation_list[self.action]) - 1 # else: self.frame_index = 0 self.hit = False # check if an attack was executed # if self.action == 3 or self.action ==4: # self.attacking = False # self.attack_cooldown = 20 ## check if damage was taken # if self.action == 5: # create attack space def high_attack(self, surface, target): attacking_rect = pygame.Rect(self.rect.centerx - (1.5 * self.rect.width * self.flip), self.rect.y, 1.5 * self.rect.width, self.rect.height / 2) if attacking_rect.colliderect(target.rect): target.health -= 10 target.hit = True pygame.draw.rect(surface, (0, 255, 0), attacking_rect) self.attack_cooldown = 20 def low_attack(self, surface, target): attacking_rect = pygame.Rect(self.rect.centerx - (1.5 * self.rect.width * self.flip), self.rect.centery, 1.5 * self.rect.width, self.rect.height / 2) if attacking_rect.colliderect(target.rect): target.health -= 10 target.hit = True pygame.draw.rect(surface, (0, 255, 0), attacking_rect) self.attack_cooldown = 20 def shooting_attack(self, surface, target): attacking_rect = pygame.Rect(self.rect.centerx - (0.5 * self.rect.width * self.flip) + 50 * self.shoot - (100 * self.shoot * self.flip), (self.rect.centery - 50), 0.5 * self.rect.width, self.rect.height / 4) if attacking_rect.colliderect(target.rect): target.health -= 5 target.hit = True pygame.draw.rect(surface, (0, 255, 0), attacking_rect) def update_action(self, new_action): # check if the new action is different to previous one if new_action != self.action: self.action = new_action # update animation settings self.frame_index = 0 self.update_time = pygame.time.get_ticks() def draw(self, surface): img = pygame.transform.flip(self.image, self.flip, False) # pygame.draw.rect(surface, (255, 255, 255), self.rect) surface.blit(img, (self.rect.x - (self.offset[0] * self.image_scale), self.rect.y - (self.offset[1] * self.image_scale))) # blit from tutorial
@HammerHeadGameStudio
@HammerHeadGameStudio 2 жыл бұрын
import pygame class Fighter(): def __init__(self, player, x, y, data, sprite_sheet, animation_steps): # add flip variable? and change False to to flip self.size = data[0] self.image_scale = data [1] self.offset = data [2] self.player = player self.flip = False self.animation_list = self.load_images(sprite_sheet, animation_steps) self.action = 0 # 0 = idle, etc. self.frame_index = 0 self.image = self.animation_list[self.action][self.frame_index] self.update_time = pygame.time.get_ticks() # or not? self.rect = pygame.Rect((x, y, 80, 180)) self.vel_y = 0 self.running = False self.jump = False self.attacking = False self.attack_type = 0 self.attack_cooldown = 0 self.hit = False self.health = 100 self.alive = True self.shoot = 0 def load_images(self, sprite_sheet, animation_steps): # continue from tutorial # extract images from sprite_sheet animation_list = [] for y, animation in enumerate(animation_steps): temp_img_list = [] for x in range(animation): temp_img = sprite_sheet.subsurface(x * self.size, y * self.size, self.size, self.size) temp_img_list.append(pygame.transform.scale(temp_img, (self.size * self.image_scale, self.size * self.image_scale))) animation_list.append(temp_img_list) return animation_list def move(self, screen_width, screen_height, surface, target, round_over): SPEED = 10 GRAVITY = 2 dx = 0 dy = 0 self.running == False self.attack_type = 0 # get key presses key = pygame.key.get_pressed() # Can only perform other actions if not attacking ## or dead if self.health = 8: self.attack_cooldown = 50 self.shoot = 0 self.attacking = False
2 Python Developers VS $1000
15:04
PolyMars
Рет қаралды 1,8 МЛН
Oh, wait, actually the best Wordle opener is not “crane”…
10:53
A teacher captured the cutest moment at the nursery #shorts
00:33
Fabiosa Stories
Рет қаралды 48 МЛН
Самый Молодой Актёр Без Оскара 😂
00:13
Глеб Рандалайнен
Рет қаралды 12 МЛН
Best KFC Homemade For My Son #cooking #shorts
00:58
BANKII
Рет қаралды 66 МЛН
IQ Level: 10000
00:10
Younes Zarou
Рет қаралды 4,7 МЛН
A new way to generate worlds (stitched WFC)
10:51
Watt Designs
Рет қаралды 519 М.
I made Games with Python for 10 Years...
28:52
DaFluffyPotato
Рет қаралды 298 М.
Can You Beat Hitman 3 Without Breaking ANY Laws?
22:37
windowledge
Рет қаралды 1,6 МЛН
3 Hours vs. 3 Years of Blender
17:44
Isto Inc.
Рет қаралды 4,4 МЛН
Someone improved my code by 40,832,277,770%
28:47
Stand-up Maths
Рет қаралды 2,4 МЛН
What comes after forever?
12:29
Random Andgit
Рет қаралды 25 М.
C++ Developer Learns Python
9:26
PolyMars
Рет қаралды 2,7 МЛН
The True Story of How GPT-2 Became Maximally Lewd
13:54
Rational Animations
Рет қаралды 1,7 МЛН
A teacher captured the cutest moment at the nursery #shorts
00:33
Fabiosa Stories
Рет қаралды 48 МЛН