Adding enemies to a pygame platformer

2024/10/5 14:50:59

I'm new to pygame and trying to make a platformer game that's based on this tutorial: http://programarcadegames.com/python_examples/show_file.php?file=platform_scroller.py

I can't quite figure out how to add moving enemies, can you help me?

Answer

Moving enemies would be something of a combination of how the Player and Platform objects work in the example to which you linked:

  1. The enemy class would be a subclass of pygame.sprite.Sprite, similar to both aforementioned objects.

  2. They would have to implement an update() method, similar to Player, to define how they move on each frame. Look at Player.update() for guidance; basically, move the Enemy's rect in some way.

  3. Instances of the enemy class should be added to a level's enemy_list object (which already exists in the example code), which means they would be updated and drawn on every frame. This is similar to how Level_0x constructors add Platform instances to the level's platform_list variable.

In short, that would look something like:

class Enemy(pygame.sprite.Sprite):def __init__(self):# Set the size, look, initial position, etc. of an enemy here...passdef update(self):# Define how the enemy moves on each frame here...passclass Level_01(Level):def __init__(self, player):# platform code already in example goes here...# Add two enemies to the levelself.enemy_list.add(Enemy())self.enemy_list.add(Enemy())
https://en.xdnf.cn/q/120269.html

Related Q&A

Tips for cleaning up a challenges answer? Weighted Sum of Digits [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…

Get a variable as filename from python script and use it in a batch script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

Python 3.x AttributeError: NoneType object has no attribute groupdict

Being a beginner in python I might be missing out on some kind of basics. But I was going through one of the codes from a project and happened to face this :AttributeError: NoneType object has no attri…

importing images from local folder instead of using Keras/Tensorflow dataset

Hi Can someone please help me to change this code so that it wont get data from keras mnist. instead it will be getting data from local folder. where do i need to make changes in it. and where in this …

why I have negative date by subtraction of two column?

Im trying to create a column his values is the subtraction of two column but I found strange values:Patient["Waiting"] = Patient["Appointment"] - Patient["Scheduled"]Sched…

Python 3: AttributeError: int object has no attribute choice [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Scraping web pages with Python vs PHP? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

error switching to iframe selenium python

Currently Im attempting to switch to iframe/fancybox, but im getting the following error:line 237, in check_response raise exception_class (message, screen, stacktrace) selenium.common.exceptions.WebDr…

SQL Date Variables in Python

I am writing a query inside a Python script.The query is as follows:cur = conn.cursor() query1 = """select max(date_time) from tablename""" cur.execute(queryy1) conn.commi…