Pygame delete non sprite objects - python

So Im trying to make an upward scrolling platformer, and I need a way to delete the score counter and hide the death screen when a player dies and restarts. However, those objects aren't sprites. Is there still a way to delete them?

this is just an example because you have not provided any code :'(
if not ondeathscreen:
#draw your deathscreen
if not inGame:
#draw your score counter
you have to personilize this code with your game but hopefully it helps :))).

Related

Pygame, is there a way to make "screen layers"?

I am working on a Tank game as a school project and I am trying to make it as user-friendly as possible (with things like customizable keybindings, display settings). However, I don't want the game window to lose it proportions so I figured I would add borders to the sides of the game window in order to support wide screens.
I've attached an imageto illustrate what I'm looking for:
So for this to work, I would need a way to make "screen layers". The base layer is the whole screen with some graphics on the sides added to it and a font displaying score. And then a second layer would be rendered in the middle of the screen which will be the game window and its width and height will be obviously smaller than the base layer.
At the moment, the game is rendered from the top left corner of the screen until all the tiles have been rendered out. So that is why I figured it would be the best to make the tiles render out on a separate surface positioned in the middle of the screen surface.
Would really appreciate some tips on how I can make this possible! :) I don't have that much experience in Classes and pygame, so it would be awesome if you could give a fleshed out description as well! :D
Just draw your different stuff on different surfaces.
For example, create a Surface for your game window (let's call it game_surf), and draw your tanks and bullets etc. on that new Surface instead of the pygame main window (which is just another Surface).
# the main window
screen = pygame.display.init()
# just a regular surface
game_surf = pygame.surface.Surface(width, height)
...
# in your main loop
# or however you draw your sprites
your_sprite_group.draw(game_surf)
Then blit it onto the screen
screen.blit(game_surf, the_position)
Simplest way: Create a group for each 'layer'.
import pygame
front_sprites = pygame.sprite.Group()
middle_sprites = pygame.sprite.Group()
background_sprites = pygame.sprite.Group()
Then, just draw them all onto the screen in the following order:
background_sprites.draw(screen)
middle_sprites.draw(screen)
front_sprites.draw(screen)

python variable not being reset

I'm currently making a game with pygame. I have one issue with my game at the moment.
The scrolling of the screen is fine, but once the image has been scrolled completely after its second time (it works fine first time) the screen blit goes all weird, all the sprites in the game leaves massive trails behind where it was previously (check screnshots). This is simply because for some stragnge reason the variable "x" is not being reset back to 0 once it has exceeded the screenwidth after the second time, it resets after it equal 1384 the first time but not after the second time..
any help is greatly appreciated.
http://pastebin.com/ub6gi8Zn (pastebin code gone)
Blit working fine before background has repeated itself twice SCREENSHOT
gyazo.com/aa5626d4927b0b9299ce2ec42c9ba501 -- after the background repeating itself twice-- sorry couldn't add more than 2 links
The problem is that you aren't clearing the screen buffer.
That is, the sprites and background just get redrawn over themselves because you haven't erased the pixels of the last time you drew those sprites on the screen!
I have never used pygame before but this is a general graphics programming problem, I tried looking up and it seems the function you are looking for is screen.fill(some_color). You fill the entire screen with a given color so the output of the last drawn frame disappear.
By the way this also happens because your "background" doesn't fill the entire area of the screen.

how to add interface in python, with pygame

Help! I am new to programming! I am trying to make a monopoly game. My program so far creates player and card objects, these objects have some values stored.
I am trying to use pygame to implement some kind of animations and create a menu bar that pops up on the pygame screen, but I don't know how to get started. So far all I have is a pygame background screen, I want to know how I can place a small rectangle on the pygame screen given position, depending on the players object's position value. I need to know how I can have all that code I have written just for the game logic, and have it run along side with this pygame stuff.
I really need to know how to get started. If anyone is willing to spend some time with me, to get me going in the right direction, I would really appreciate it, thanks!
Reading over some pygame tutorials will certainly help.
The flow of your program will eventually look something like this:
# This is intentionally simplified pseudocode, but at the bottom-most level most
# simple games have a similar structure to this.
#Construct your game objects
players = [Player(args) for n in range(num_players)]
cards = loadCards() #or however you load your cards (files,database,etc)
board = Board()
#initialize your display context
pygame_screen.init() #or whatever is the correct syntax for Pygame
# Main game loop
while 1:
check_inputs() #get key/mouse input inputs for this frame
handle_inputs() #handle each input that has occurred during this frame
update_gamestate() #update the state on the game (who's turn, etc)
update_display() #update pygame window graphics (draw stuff, flip display)
cleanup()
As for the details on how to draw rectangles and whatnot, you should read about the various draw functions here

Pygame Collision on Rect

I'm a student in Computer Programming at my high school and really need help to make the square an obstacle that ships can't enter and bullets disappear once you shoot at the square also for the circle the same thing. And both ships die if they collide with each other. Please help I know my question isn't the best worded but i really need help.
the link to the code is here https://docs.google.com/file/d/0B-Pb_T-Vgr3-TnhRY0lvVjJHX0U/edit?usp=sharing
I would suggest that you create a GameObject class which extends pygame.sprite.Sprite and both Ship and Bullet classes which extend GameObject. This allows you to then easily add properties that both will need, such as velocity and acceleration, and you can create a collide method that is overridden by both for specific behavior. The added bonus here is that pygame.sprite.collide_rect will work properly as such:
if (pygame.sprite.collide_rect(sprite1, sprite2)):
# sprite1 and sprite2 are colliding!
# do something, such as calling sprite1.collide(sprite2)
# and sprite2.collide(sprite1)
So, pygame.sprite.collide_rect checks if two sprites collide using the Sprite.rect property of each. This functionality could also be recapitulated using rect.colliderect:
sprite1.rect.colliderect(sprite2.rect)

How would you draw multiple screens in PyGame (in only one window)?

I'm trying to port over some code from Javascript to Python, and I'm having trouble even figuring out where to start. I've looked at a good dozen tutorials for PyGame, but none of them seem to click for me. So, I was hoping to get a quick example here, or at least a point in the right direction.
I'm wanting to make a number of screens I can switch back and forth between, depending on what the user is doing at the time, and even display two side by side. At the moment, all I've got is some Javascript that draws random circles onto the screen. The PyGame logic is the only thing I'm having trouble with.
Here's my javascript for reference.
You can create a Subsurface for each subscreen that you want to create.
Then you may treat each as if it were a full screen / single surface, yet they still reference the original screen.
Pygame is a wrapper for SDL. SDL uses a surface to represent a bitmap, or anything that can be drawn on screen. With the pygame.display.set_mode((w,h),0,d) you can get the surface, or the whole canvas. You can then draw or blit the other surfaces and then call flip(), to show changes. If you wish to have a few screens, you could have a current state number, and blit the screens accordingly.
For example:
if(current_state == MAIN_SCREEN):
drawAll(screen)
else
drawEnemiesOnly(screen)
you could change the screens with the number keys:
for event in pygame.event.get():
if event.type == KEY_DOWN:
if(event.key == K_1):
current_state = 1

Categories

Resources