def update(self, delta_time): """ All the logic to move, and the game logic goes here. also called animate method """ self.ball.center_x += 3 #move to the right if check_for_collision (self.ball, self.player_paddle) == True: print ('hit') self.ball.center_x = 0
def update(self, delta_time): """ All the logic to move, and the game logic goes here. """ print(delta_time) self.ball.center_x += self.direction_x * self.speed # move to the right self.ball.center_y += self.direction_y * self.speed # move up and down # next make ball start from random spot move in random direction # bounce off walls and paddle. if check_for_collision(self.ball, self.player_paddle) == True: self.score += 10 #print (self.score) arcade.play_sound(self.paddle_sound) # keeps ball from bouncing back and forth when it hits paddle end self.ball.center_x = SCREEN_WIDTH - SCREEN_WIDTH / 10 self.direction_x *= -1 print(self.ball.center_x) elif self.ball.center_x > SCREEN_WIDTH: self.direction_x *= -1 self.score -= 1 arcade.play_sound(self.ball_sound) elif self.ball.center_x < 10: self.direction_x *= -1 arcade.play_sound(self.ball_sound) elif self.ball.center_y > SCREEN_HEIGHT or self.ball.center_y < 10: self.direction_y *= -1 arcade.play_sound(self.ball_sound) elif self.score > 20: print('Winner') sleep(2) exit()
def update(self, delta_time): """ All the logic to move, and the game logic goes here. """ # print (delta_time) self.ball.center_x += self.direction_x * self.speed * delta_time # move to the right self.ball.center_y += self.direction_y * self.speed * delta_time # move up and down # next make ball start from random spot move in random direction # bounce off walls and paddle. if check_for_collision(self.ball, self.player_paddle) == True: self.score += 10 arcade.play_sound(self.paddle_sound) # keeps ball from bouncing back and forth when it hits paddle end #self.ball.center_x = SCREEN_WIDTH - SCREEN_WIDTH / 10 self.direction_x *= -1 elif self.ball.center_x > SCREEN_WIDTH: # player misses ball arcade.play_sound(self.lose_sound) arcade.pause(2) self.score -= 1 self.start_ball() elif self.ball.center_x < 10: self.direction_x *= -1 arcade.play_sound(self.ball_sound) elif self.ball.center_y > SCREEN_HEIGHT or self.ball.center_y < 10: self.direction_y *= -1 arcade.play_sound(self.ball_sound) elif self.score > 10: print('Winner') arcade.draw_text('You Win! ', 100, 100, arcade.color.RED, 36) arcade.start_render() arcade.pause(5) exit()
def update(self, delta_time): """ All the logic to move, and the game logic goes here. """ self.ball.center_x += self.direction_x * self.speed # move to the right self.ball.center_y += self.direction_y * self.speed # move up and down # next make ball start from random spot move in random direction # bounce off walls and paddle. if check_for_collision(self.ball, self.player_paddle) == True: self.score += 10 arcade.play_sound(self.paddle_sound) # keeps ball from bouncing back and forth when it hits paddle end #self.ball.center_x = WIDTH - WIDTH / 10 self.direction_x *= -1 elif self.ball.center_x > WIDTH: # player misses ball arcade.play_sound(self.lose_sound) arcade.pause(2) self.score -= 1 self.start_ball() elif self.ball.center_x < 10: self.direction_x *= -1 arcade.play_sound(self.ball_sound) elif self.ball.center_y > HEIGHT or self.ball.center_y < 10: self.direction_y *= -1 arcade.play_sound(self.ball_sound) elif self.score > 15: print('Winner') game_over_view = GameOverView(self.score) self.window.show_view(game_over_view)
def collides_with_sprite(self, other: 'Sprite') -> bool: """Will check if a sprite is overlapping (colliding) another Sprite. Args: self: Current Sprite. other: The other sprite to check against. Returns: True or False, whether or not they are overlapping. """ from arcade.geometry import check_for_collision return check_for_collision(self, other)
def update(self): """ Move everything and resolve collisions. """ # print(f"Spot A ({self.player_sprite.center_x}, {self.player_sprite.center_y})") # --- Add gravity self.player_sprite.change_y -= self.gravity_constant # --- Move in the y direction self.player_sprite.center_y += self.player_sprite.change_y # Check for wall hit hit_list = check_for_collision_with_list(self.player_sprite, self.platforms) # If we hit a wall, move so the edges are at the same point if len(hit_list) > 0: if self.player_sprite.change_y > 0: for item in hit_list: self.player_sprite.top = min(item.bottom, self.player_sprite.top) # print(f"Spot X ({self.player_sprite.center_x}, {self.player_sprite.center_y})") elif self.player_sprite.change_y < 0: for item in hit_list: while check_for_collision(self.player_sprite, item): # self.player_sprite.bottom = item.top <- Doesn't work for ramps self.player_sprite.bottom += 0.25 if item.change_x != 0: self.player_sprite.center_x += item.change_x # print(f"Spot Y ({self.player_sprite.center_x}, {self.player_sprite.center_y})") else: pass # TODO: The code below can't execute, as "item" doesn't # exist. In theory, this condition should never be arrived at. # Collision while player wasn't moving, most likely # moving platform. # if self.player_sprite.center_y >= item.center_y: # self.player_sprite.bottom = item.top # else: # self.player_sprite.top = item.bottom self.player_sprite.change_y = min(0.0, hit_list[0].change_y) # print(f"Spot B ({self.player_sprite.center_x}, {self.player_sprite.center_y})") self.player_sprite.center_y = round(self.player_sprite.center_y, 2) # print(f"Spot Q ({self.player_sprite.center_x}, {self.player_sprite.center_y})") # --- Move in the x direction self.player_sprite.center_x += self.player_sprite.change_x check_again = True while check_again: check_again = False # Check for wall hit hit_list = check_for_collision_with_list(self.player_sprite, self.platforms) # If we hit a wall, move so the edges are at the same point if len(hit_list) > 0: change_x = self.player_sprite.change_x if change_x > 0: for item in hit_list: # print(f"Spot 1 ({self.player_sprite.center_x}, {self.player_sprite.center_y})") # See if we can "run up" a ramp self.player_sprite.center_y += change_x if len( check_for_collision_with_list( self.player_sprite, self.platforms)) > 0: self.player_sprite.center_y -= change_x self.player_sprite.right = min( item.left, self.player_sprite.right) # print(f"Spot R ({self.player_sprite.center_x}, {self.player_sprite.center_y})") check_again = True break # else: # print("Run up ok 1") # print(f"Spot 2 ({self.player_sprite.center_x}, {self.player_sprite.center_y})") elif change_x < 0: for item in hit_list: # See if we can "run up" a ramp self.player_sprite.center_y -= change_x if len( check_for_collision_with_list( self.player_sprite, self.platforms)) > 0: # Can't run up the ramp, reverse self.player_sprite.center_y += change_x self.player_sprite.left = max( item.right, self.player_sprite.left) # print(f"Reverse 1 {item.right}, {self.player_sprite.left}") # Ok, if we were shoved back to the right, we need to check this whole thing again. check_again = True break # print(f"Spot 4 ({self.player_sprite.center_x}, {self.player_sprite.center_y})") else: print( "Error, collision while player wasn't moving.\nMake sure you aren't calling multiple updates, like a physics engine update and an all sprites list update." ) # print(f"Spot E ({self.player_sprite.center_x}, {self.player_sprite.center_y})") for platform in self.platforms: if platform.change_x != 0 or platform.change_y != 0: platform.center_x += platform.change_x if platform.boundary_left is not None \ and platform.left <= platform.boundary_left: platform.left = platform.boundary_left if platform.change_x < 0: platform.change_x *= -1 if platform.boundary_right is not None \ and platform.right >= platform.boundary_right: platform.right = platform.boundary_right if platform.change_x > 0: platform.change_x *= -1 if check_for_collision(self.player_sprite, platform): if platform.change_x < 0: self.player_sprite.right = platform.left if platform.change_x > 0: self.player_sprite.left = platform.right platform.center_y += platform.change_y if platform.boundary_top is not None \ and platform.top >= platform.boundary_top: platform.top = platform.boundary_top if platform.change_y > 0: platform.change_y *= -1 if platform.boundary_bottom is not None \ and platform.bottom <= platform.boundary_bottom: platform.bottom = platform.boundary_bottom if platform.change_y < 0: platform.change_y *= -1
def tree_hit(self): for platform in self.platforms: if platform.is_tree: hit = check_for_collision(self.player_sprite, platform) if hit: self.player_sprite.is_dead = True
def update(self): """ Move everything and resolve collisions. """ # --- Add gravity self.player_sprite.change_y -= self.gravity_constant # --- Move in the y direction self.player_sprite.center_y += self.player_sprite.change_y # Check for wall hit hit_list = \ check_for_collision_with_list(self.player_sprite, self.platforms) # If we hit a wall, move so the edges are at the same point if len(hit_list) > 0: if self.player_sprite.change_y > 0: for item in hit_list: self.player_sprite.top = min(item.bottom, self.player_sprite.top) elif self.player_sprite.change_y < 0: for item in hit_list: while check_for_collision(self.player_sprite, item): self.player_sprite.bottom += 0.5 if item.change_x != 0: self.player_sprite.center_x += item.change_x else: pass # TODO: The code below can't execute, as "item" doesn't # exist. In theory, this condition should never be arrived at. # Collision while player wasn't moving, most likely # moving platform. # if self.player_sprite.center_y >= item.center_y: # self.player_sprite.bottom = item.top # else: # self.player_sprite.top = item.bottom self.player_sprite.change_y = min(0, hit_list[0].change_y) # --- Move in the x direction self.player_sprite.center_x += self.player_sprite.change_x # Check for wall hit hit_list = \ check_for_collision_with_list(self.player_sprite, self.platforms) # If we hit a wall, move so the edges are at the same point if len(hit_list) > 0: change_x = self.player_sprite.change_x if change_x > 0: for item in hit_list: # See if we can "run up" a ramp self.player_sprite.center_y += change_x if check_for_collision(self.player_sprite, item): self.player_sprite.center_y -= change_x self.player_sprite.right = \ min(item.left, self.player_sprite.right) elif change_x < 0: for item in hit_list: # See if we can "run up" a ramp self.player_sprite.center_y -= change_x if check_for_collision(self.player_sprite, item): self.player_sprite.center_y -= change_x self.player_sprite.left = max(item.right, self.player_sprite.left) else: print("Error, collision while player wasn't moving.") for platform in self.platforms: if platform.change_x != 0 or platform.change_y != 0: platform.center_x += platform.change_x if platform.boundary_left is not None \ and platform.left <= platform.boundary_left: platform.left = platform.boundary_left if platform.change_x < 0: platform.change_x *= -1 if platform.boundary_right is not None \ and platform.right >= platform.boundary_right: platform.right = platform.boundary_right if platform.change_x > 0: platform.change_x *= -1 if check_for_collision(self.player_sprite, platform): if platform.change_x < 0: self.player_sprite.right = platform.left if platform.change_x > 0: self.player_sprite.left = platform.right platform.center_y += platform.change_y if platform.boundary_top is not None \ and platform.top >= platform.boundary_top: platform.top = platform.boundary_top if platform.change_y > 0: platform.change_y *= -1 if platform.boundary_bottom is not None \ and platform.bottom <= platform.boundary_bottom: platform.bottom = platform.boundary_bottom if platform.change_y < 0: platform.change_y *= -1
def update(self): """ Move everything and resolve collisions. """ # --- Add gravity self.player_sprite.change_y -= self.gravity_constant # --- Move in the y direction self.player_sprite.center_y += self.player_sprite.change_y # Check for wall hit hit_list = \ check_for_collision_with_list(self.player_sprite, self.platforms) # If we hit a wall, move so the edges are at the same point if len(hit_list) > 0: if self.player_sprite.change_y > 0: for item in hit_list: self.player_sprite.top = min(item.bottom, self.player_sprite.top) elif self.player_sprite.change_y < 0: for item in hit_list: while check_for_collision(self.player_sprite, item): self.player_sprite.bottom += 0.5 if item.change_x != 0: self.player_sprite.center_x += item.change_x else: pass # TODO: The code below can't execute, as "item" doesn't # exist. In theory, this condition should never be arrived at. # Collision while player wasn't moving, most likely # moving platform. # if self.player_sprite.center_y >= item.center_y: # self.player_sprite.bottom = item.top # else: # self.player_sprite.top = item.bottom self.player_sprite.change_y = min(0, hit_list[0].change_y) # --- Move in the x direction self.player_sprite.center_x += self.player_sprite.change_x # Check for wall hit hit_list = \ check_for_collision_with_list(self.player_sprite, self.platforms) # If we hit a wall, move so the edges are at the same point if len(hit_list) > 0: change_x = self.player_sprite.change_x if change_x > 0: for item in hit_list: # See if we can "run up" a ramp self.player_sprite.center_y += change_x if check_for_collision(self.player_sprite, item): self.player_sprite.center_y -= change_x self.player_sprite.right = \ min(item.left, self.player_sprite.right) elif change_x < 0: for item in hit_list: # See if we can "run up" a ramp self.player_sprite.center_y -= change_x if check_for_collision(self.player_sprite, item): self.player_sprite.center_y -= change_x self.player_sprite.left = max(item.right, self.player_sprite.left) else: print("Error, collision while player wasn't moving.") for platform in self.platforms: if platform.change_x != 0 or platform.change_y != 0: platform.center_x += platform.change_x if platform.boundary_left is not None \ and platform.left <= platform.boundary_left: platform.left = platform.boundary_left if platform.change_x < 0: platform.change_x *= -1 if platform.boundary_right is not None \ and platform.right >= platform.boundary_right: platform.right = platform.boundary_right if platform.change_x > 0: platform.change_x *= -1 if check_for_collision(self.player_sprite, platform): if platform.change_x < 0: self.player_sprite.right = platform.left if platform.change_x > 0: self.player_sprite.left = platform.right platform.center_y += platform.change_y if platform.boundary_top is not None \ and platform.top >= platform.boundary_top: platform.top = platform.boundary_top if platform.change_y > 0: platform.change_y *= -1 if platform.boundary_bottom is not None \ and platform.bottom <= platform.boundary_bottom: platform.bottom = platform.boundary_bottom if platform.change_y < 0: platform.change_y *= -1
def animate(self, delta_time): #check timed events self.item_time_diff = time.time() - self.start_time self.invis_time_diff = time.time() - self.invis_timer if self.invis_time_diff > 1: self.invis_activated = False self.invis_timer = 0 #gen items if self.item_time_diff > 10: tmp_coordinates = (self.player_sprite.get_points()) item_choice = random.choice(['rocket', 'lightning', 'invis']) self.last_item = item_choice x = random.randint(0, self.player_sprite.center_x - self.player_sprite.width / 2 - 20) \ if tmp_coordinates[0][0] >= 25 else tmp_coordinates[1][0] + random.randint(50, 2200) y = random.randint(self.player_sprite.center_x + self.player_sprite.width / 2 + 20, SCREEN_WIDTH) \ if tmp_coordinates[1][0] <= SCREEN_WIDTH - 25 else tmp_coordinates[0][0] - random.randint(50, 200) item_dict = { 'rocket': self.rocketGen(random.choice([x, y]), 50), 'lightning': self.lightningGen(random.choice([x, y]), 50), 'invis': self.invisGen(random.choice([x, y]), 50) } self.item_sprites_list.append(item_dict[item_choice]) self.item_time_diff = 0 self.start_time = time.time() #computer logic if self.ball_sprite.center_x > self.computer_sprite.center_x and \ self.computer_sprite.center_x + self.computer_sprite.width / 2 < SCREEN_WIDTH: self.computer_sprite.center_x += self.board_speed_computer elif self.ball_sprite.center_x < self.computer_sprite.center_x and self.computer_sprite.center_x \ - self.computer_sprite.width / 2 > 0: self.computer_sprite.center_x -= self.board_speed_computer #move player if self.last_key == 'LEFT' and self.player_sprite.center_x - self.player_sprite.width / 2 > 0: self.player_sprite.center_x -= self.board_speed_player elif self.last_key == 'RIGHT' and self.player_sprite.center_x + self.player_sprite.width / 2 <= SCREEN_WIDTH: self.player_sprite.center_x += self.board_speed_player self.ball_sprite.center_x += self.ball_velocity_x self.ball_sprite.center_y += self.ball_velocity_y #check collision if check_for_collision(self.ball_sprite, self.player_sprite) or check_for_collision(self.ball_sprite, self.computer_sprite): self.contact_counter += 1 self.paddle_ball_dif = math.fabs(self.ball_sprite.center_x - self.player_sprite.center_x) if self.ball_sprite.center_y < SCREEN_WIDTH /2 \ else math.fabs(self.ball_sprite.center_x - self.computer_sprite.center_x) self.ball_velocity_y *= -1 if self.ball_velocity_x > 0: self.ball_velocity_x = (self.paddle_ball_dif * 0.0133333 + 0.75) * math.fabs(self.ball_velocity_y) else: self.ball_velocity_x = (self.paddle_ball_dif * 0.0133333 + 0.75) * math.fabs(self.ball_velocity_y) * -1 #check itemPickup collision_list = arcade.geometry.check_for_collision_with_list(self.player_sprite, self.item_sprites_list) for item in collision_list: self.item_sprites_list.remove(item) if self.last_item == 'rocket': self.rocket_count += 1 elif self.last_item == 'lightning': self.lightning_count += 1 elif self.last_item == 'invis': self.invis_count += 1 #check screenborders if self.ball_sprite.center_x <= 0 or self.ball_sprite.center_x >= SCREEN_WIDTH: self.ball_velocity_x *= -1 if self.contact_counter >= 2: self.ball_velocity_y += 0.5 if self.ball_velocity_y > 0 else -0.5 self.contact_counter = 0 #check if game over if self.ball_sprite.center_y < 0: self.computer_score+= 1 self.resetGame() elif self.ball_sprite.center_y > SCREEN_HEIGHT: self.player_score += 1 self.resetGame() #check item usage if self.rocket_activate is True and self.rocket_count > 0: self.rocketGen(self.player_sprite.center_x, self.player_sprite.center_y) self.rocket_activate = False self.rocket_sprites_list.append(self.rocketGen(self.player_sprite.center_x, self.player_sprite.center_y)) self.rocket_count -= 1 self.all_sprites_list.update() #update item pos for rocket in self.rocket_sprites_list: rocket.center_y += rocket.change_y if check_for_collision(rocket, self.computer_sprite): tmp_x = self.computer_sprite.center_x tmp_y = self.computer_sprite.center_y self.all_sprites_list.remove(self.computer_sprite) self.computer_sprite = arcade.Sprite("enemy_short.png", SPRITE_SCALE) self.all_sprites_list.append(self.computer_sprite) self.computer_sprite.center_x = tmp_x self.computer_sprite.center_y = tmp_y self.rocket_sprites_list.remove(rocket)