My Character Sprite Doesn't Move Off Screen - python

I have been going over Paul Craven's Python and Pygame tutorial and lately I am trying to understand everything in Sprites. So, being inspired by Craven's multiple levels code, I've written my own code on it and it worked fine.
The logic is, if player sprite is off screen(for example screen width is equal to 640 and player's x value is greater than 640, than you are in the next level). Than, I decided to add menu to my game(?) and menu works fine as well.
The problem is, now my character can't get to second level. It seems it's x value is unable to pass 640, and I don't know why.
Since I am not able to use Stackoverflow editor good enough to share Python code(you know, intended), I will be using Pastebin to share my code.
http://pastebin.ubuntu.com/10392389/
Stackoverflow doesn't let me post the image files I've used, but they are classic pictures I got from the internet.
Any help is appreciated, thanks.

if current_level_num == 1 and player.rect.x > SCREEN_WIDTH:
current_level_num = 2
current_level = levels[current_level_num]
player.rect.x = 0
if current_level_num == 2 and player.rect.x < 1:
current_level_num = 1
current_level = levels[current_level_num]
player.rect.x = SCREEN_WIDTH
The character moves to the 2nd level, then immediately moves back to the 1st. The player should be moved farther to the right than the level 2 boundary.

Related

Making a Bullet move towards a Target without following the Target using Pgzhelper in Pygame Zero

Question: How can I make a bullet move towards a target without following the target using Pgzhelper in Pygame Zero?
I wanted to share this with a lot of people that may have the same trouble using Pygame Zero:
The problem may stem from people confusing how to move Actors using angle(), distance(), direction(), direction_to(), move_forward(), move_backward(), move_towards() and so on. I've watched and read a lot of videos and read many formulaic solutions but did not understand the key points of them because I suck hard at basic trigonometry.
So here's a simple solution, I realize after a few days:
ammo = []
def reload():
bullet = Actor('pic', center = (x,y))
bullet.angle = bullet.direction_to(target)
ammo.append(bullet)
def update():
for bullet in ammo:
bullet.move_forward(15)
if bullet.x < 0 or bullet.x > WIDTH or bullet.x < 0 or bullet.y > HEIGHT:
ammo.remove(bullet)
And after realizing this I LOL myself for how simple the solution was. Hahaha XD
I hope this would help others that suck at Trig or Math like me and are searching for simple ways to program games "as efficiently as possible".

Is there a way to keep an object in bounds in pygame, and make it move when you hold the directional keys down?

I am using pygame 1.9.6 and python 3.7.7. I am looking for a way to have something drawn by pygame(or maybe even an image) that stays inside the window border(and stops when I release the key. I started with the “if keys[]:” method:
if keys[pygame.K_LEFT] and x>0:
x-=speed
that made it stay in bounds, but it only moved a few pixels and then did not repeat.
Next I tried the “event.key” method:
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_LEFT:
xchange=-speed
ychange=-0
but that just makes the object move forever. I tried putting “and x>0” on the same line right after the direction, an “if x>0:” before, an “if x<0:” after, but the event never updates to see that the coordinates are past the edge, even with update commands. It just keeps going and going. I also don’t know how to make the object stop moving when I release the key, since event.key’s have the event always on.
Thank you for all the help you can offer.
In order for something to stay within a frame, you must check if it's coordinates are not past the border before moving it.
keys = pygame.key.get_pressed()
if(keys[K_LEFT]):
if(square.left > 0):
square.left -= 10
if(keys[K_RIGHT]):
if(square.right < 600):
square.left += 10
if(keys[K_UP]):
if(square.top > 0):
square.top -= 10
if(keys[K_DOWN]):
if(square.bottom < 600):
square.bottom += 10
This is an example of how you would do this with a Rect object, it checks for border and then moves the object if its within those border and the appropriate key is being pressed. I can't apply this to your code as I cannot see enough of it but you just have to replace the coordinates. For example, you might have thing.x instead of square.left

name 'delta_scroll' is not defined even though I defined it?

so I am trying to make my enemy not scroll with my camera when it moves left and right
I someone helped me make a function to stop my enemy from scrolling all it suppose to do is not make my enemy scroll example vid
def onscroll(enemying, delta_scroll):
for enemys in enemying:
enemys.position = (enemys.position[0] - delta_scroll, enemys.position[1])
then on my main loop I called that function so my enemy doesnt scroll with my screen
# camera left and right movement
if playerman.y < 250:
playerman.y += 1
for enemys in enemying:
enemys.y += delta_scroll
but for some reason I keep getting the same error
name 'delta_scroll' is not defined
Enemy Class
my full code
Quick tip- there are a TON of pg.image.load("myImg.png") commands. here is a better way of doing it:
standingright = []
for i in range(1, 16):
imgPath = "d"+str(i)+".png"
img = pg.image.load(imgPath)
standingright.append(img)
This is a much more concise way of loading the images. It will result in the exact same list of images in the exact same order, but I thought I would mention it to you because it will make your code much neater.
About the issue at hand: I searched your code for mentions of delta_scroll, and found it being used, but never defined. Also I saw you defined the function "onscroll", but never used it. Are you getting these two mixed up perhaps?

Collisions w/ Tiles on Tile Map Python 3.6

I am making a small game for my AP Econ class and have been following a tutorial to create a tile map and am having difficulty with collisions. I have searched through the questions on this site as well as anywhere else that I could find programming related questions (Primarily here) but have not been able to find any that are don't have a map built by hand (Building a map by hand in a text document or directly in the python file with a list). I am new at python so the answer might be fairly straight forward XD.
Main Program: https://pastebin.com/VbGVzJab
Textures: https://pastebin.com/iTBpiub3
Colors: https://pastebin.com/ywMKv6Z1
Globals: https://pastebin.com/E40a7Kss
Map_Engine: https://pastebin.com/PJgnSFJP
Map_Editor: https://pastebin.com/mvgaYBDF
Tutorial I have been following: https://www.youtube.com/watch?v=sxk4Oi9QOrM&t=997s
This is how my folders are set up:
My issue is that I my character (player) continues to walk instead of stopping at the water (Or whichever tile type is designated (In my case it is currently water). Thank you all in advance!!
I don't know if this will help, but you can try either making movement variables to signal if the person can move that way, and when you come across that area of coordinates (the water), you can set those coordinates to False. If you made coordinates (x and y to move your character), it shouldn't be hard.
Please note I'm just using a random coordinate area
MOVE_RIGHT = True
MOVE_LEFT = True
MOVE_UP = True
MOVE_DOWN = True
if x == 250:
MOVE_RIGHT = False
if event.key == K_RIGHT and MOVE_RIGHT == True:
x += 1
Hope this helps!

Pygame "Asteroids" Style Screen

I'm trying to design a game with screen transfer mechanics similar to Asteroids. I want it so that if you move to the edge of the screen, you appear on the other end. Here is a test code so far. The idea is to move too far right and reappear on the left. I do appear on the left, but only after I lift up on the right key.
if position_x >= 1200:
position_x = 0
You definitely should post some of your code so we know how you set position_x and how you use it, but what I can say that might help is to use modulo instead of direct resetting. So for example, when you're updating position_x - position_x = (position_x + speed) % 1200

Categories

Resources