I'm trying to make a dinosaur game in python (like the offline dino game in chrome).
I want the dino to jump when space is pressed but when I press it,not only the image of dino is duped but it also doesn't come back.
import pygame
import time
pygame.init()
displayWidth = 700
displayHeight = 350
gameDisplay = pygame.display.set_mode((displayWidth,displayHeight))
pygame.display.set_caption("Dino-Run")
black = (0,0,0)
white = (255,255,255)
clock = pygame.time.Clock()
dinoimg = pygame.image.load("dino.png")
def dino(x,y):
gameDisplay.blit(dinoimg,(x,y))
def gameloop():
gameExit = False
x = (displayWidth * 0.005)
y = (displayHeight * 0.75)
y_change = 0
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
y_change = -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
y_change = 0
y += y_change
dino(x,y)
pygame.display.update()
clock.tick(60)
Could someone please tell me how can I prevent the dino to dupe each time space is pressed and for the dino to come back to the ground.
You have to override everything before drawing new things to the screen.
Add this at the beginning of your loop:
gameDisplay.fill(color)
Related
I try to get my Pygame character on screen but I cannot get it to appear so I can control it for a game I am building
Here is the code:
import pygame
#turn on pygame
pygame.init()
#window, window name and icon
screen = pygame.display.set_mode((1920,1080))
pygame.display.set_caption("When The Sky Turns Grey")
icon = pygame.image.load('cat.png')
pygame.display.set_icon(icon)
#Player
playerIMG = pygame.image.load('cat.png')
playerX = 960
playerY = 1080
playerXchange = 0
def player():
screen.blit(playerIMG, (playerX, playerY))
#Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#KEYS
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
playerX_change = -5
if event.key == pygame.K_d:
playerX_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d:
playerXchange = 0
playerX += playerXchange
#RGB
screen.fill((0, 0, 255))
#draw player (call player function)
player()
pygame.display.update()
I cannot get it to show up at all, no matter what I change.
I am new to pygame and if anyone can help I will be grateful
Thank you!
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
There is 2 problems in this question
First your cat Y position is to much that it went under the screen, it should be like this:
playerY = 540 # Don't go over 1080 which is the screen height
Second you didn't update the display so it doesn't show anything on the screen, at the very end of the while loop put pygame.display.update after player()
I was building a simple rocket game and it required moving some sprites. In the code below cloud1 is supposed to move -30 pixels towards the bottom each time i press the K_DOWN key. I have been trying to figure out what is wrong with the code for 3 days but haven't progressed even a little bit. Help would be much appreciated.
import pygame
pygame.init()
DISPLAY_HEIGHT = 700
DISPLAY_WIDTH = 900
screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
pygame.display.set_caption('Rocket Game')
clock = pygame.time.Clock()
FPS = 60
#colors
WHITE = (255,255,255)
BLACK = (0,0,0)
SKY_BLUE = (102,178,255)
cloud1 = pygame.image.load('cloud.png')
cloud1_X, cloud1_Y = 100, 50
cloud1_Y_change = 30
def cloud1_display(x, y):
screen.blit(cloud1, (x, y))
running = True
while running:
screen.fill(SKY_BLUE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
cloud1_Y += cloud1_Y_change
cloud1_display(cloud1_X, cloud1_X)
clock.tick(FPS)
pygame.display.update()
There are two problems. The first is that your code is not checking the event.key for the pygame.K_UP. But your code is also painting the cloud at (x, x), not (x, y).
Corrected code:
while running:
screen.fill(SKY_BLUE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP: # <<-- HERE
cloud1_Y += cloud1_Y_change
cloud1_display(cloud1_X, cloud1_Y) # <<-- AND HERE
clock.tick(FPS)
pygame.display.update()
For you main game loop try to use event.key instead of event.type for the second time. Like so:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
cloud1_Y += cloud1_Y_change
Another issue that I have noticed is that your not converting your image to a rect object in pygame and then use .blit to show it onscreen. The .blit function requires a rect object argument, so that's why your having issues.
cloud1 = pygame.image.load('asteroid_pic.bmp')
rect = cloud1.get_rect()
screen.blit(cloud1, self.rect)
I also recommend creating separate classes for your sprites, so that its easier to keep track of them and if you want to create duplicates of the same one but still retain the same characteristics of the single class sprite you can do so by importing the function Group from pygame.sprite.
So, I got stuck again, but I use this as a last resort when nothing's working after extensive research. Please don't roast me for this, I am a newbie. So, basically I am trying to make my sprite move (yoyo), but the frames keep replicating as the yoyo moves up and down. So, I don't know how to fix that. If the yoyo touches the borders of the game window, it collides and it's supposed to display a text and then the game starts over again. However, when the yoyo collides with the window border, it restarts, but the yoyo that got stuck is still being displayed and a new yoyo appears. The text is displayed but doesn't go away after 2 seconds.
import pygame
import time
pygame.init()
width = 900
height = 900
red = (255,0,0)
text = "game over"
screem = pygame.display.set_mode((width,height))
pygame.display.set_caption("yoyo")
clock = pygame.time.Clock()
background = pygame.image.load("room.png").convert()
win.blit(background, [0,0])
yoyo= pygame.image.load("yoyo.png").convert()
def Yoyo (x,y):
win.blit(yoyo, [x,y])
def mainloop():
x = 87
y = 90
yc = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
Exit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
Yoyo(x,y)
y += yc
if y > 23 or y < -90:
pygame.display.update()
clock.tick(60)
mainloop()
pygame.quit()
quit()
Redraw the entire scene in every frame. This means you've to draw the background in every frame, too.
Draw (blit) the background in the main loop, before anything else is drawn:
while not Exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
Exit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change = -5
elif event.key == pygame. K_DOWN:
y_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
y += y_change
if y > 405 or y < -200:
collision()
GameLoop()
win.blit(bg, [0,0]) # <----- draw background
Bee(x,y) # <----- draw the bee on the background
# [...] all further drawing has to be done here
pygame.display.update()
clock.tick(60)
I have problem with my code, I started to make 2D tank game, the problem is the shooting bullets from the tank position.. Here's my code, check it out, I'm trying to figure it out like 3-4 hours.. I hope someone knows how to do it, thanks!:)
by the way, sorry for the creepy code, I'm new to Pygame :)
import pygame
pygame.init()
#---WINDOW----
display_width = 1000
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('powpypow')
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
background = pygame.image.load('background.png')
tank1 = pygame.image.load('tank1.png')
tankfire = pygame.image.load('tankfire.png')
shot = pygame.image.load('shot.png')
clock = pygame.time.Clock()
def tank(x,y):
gameDisplay.blit(tank1, (x,y))
x = (display_width * 0.10)
y = (display_height * 0.58)
x_change = 0
tank_width = 73
#---GAME LOOP----
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
x_change = -10
elif event.key == pygame.K_d:
x_change = 10
elif event.key == pygame.K_SPACE:
tank1 = pygame.image.load('tankfire.png')
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d or event.key == pygame.K_SPACE:
tank1 = pygame.image.load('tank1.png')
x_change = 0
if x >= display_width - tank_width or x <= 0:
x = 0
if x > display_width / 2:
x = 0
x += x_change
gameDisplay.blit(background, (0,0))
tank(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
If you are going to want to render bullets, you are going to need to keep track of them as they move across the screen. This will require some sort of list or group of bullets that you iterate through and draw, one at a time.
I recommend you have a look at a tutorial to help you switch to using Sprites in a group, rather than blitting each image onto the screen individually. You will find that it is much easier to track each object and adjust its position.
The Chimp tutorial from the Pygame website is pretty good: https://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html
I am attempting to make a game where the 'player' (who spawns as the moon furthest to the right) can move around using the 'WASD' keys and enemies fall from the top of the screen in order to hit the player and end the game. I am currently doing this for an assignment and i have set up a test to see whether it is possible for me to do this and so far i have been able to create a 'player' object that is controllable by the 'WASD' keys and make a screen which requires players to press 'SPACE' to play the game. I am having trouble though with being able to have the enemies fall on the 'Y' axis down the screen smoothly. The picture moves down the screen only when the user presses or releases a key, this creates a jagged movement or when the user moves the mouse over the pygame screen, creating a very smooth movement.
#import necessary modules and pygame.
import pygame, sys, random
from pygame.locals import *
#set global variables
pygame.init()
WINDOWWIDTH = 800
WINDOWHEIGHT = 800
BACKGROUNDCOLOUR = (255,255,255)
TEXTCOLOUR = (0,0,0)
FPS = 30
ENEMYMINSIZE = 10
BOMBSAWAY = -1
ENEMYMAXSIZE = 40
ENEMYMINSPEED = 1
ENEMYMAXSPEED = 10
ADDNEWENEMYRATE = 5
PLAYERMOVERATE = 5
FSIZE = 48
BLUE = (0,0,255)
global PLAY
PLAY = False
global fpsClock
fpsClock = pygame.time.Clock()
# set up pygame and GUI
MainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
windowSurface.fill(BACKGROUNDCOLOUR)
pygame.display.set_caption('Bubble Dash')
enemy = pygame.image.load('player.jpg')
char = pygame.image.load('player.jpg')
# set up fonts
basicFont = pygame.font.SysFont(None, 48)
# set up the text
text = basicFont.render('Press any key to play!', True, (255,255,0))
textRect = text.get_rect()
textRect.centerx = windowSurface.get_rect().centerx
textRect.centery = windowSurface.get_rect().centery
# draw the text onto the surface
# set up x and y coordinates
# music
windowSurface.blit(text, textRect)
def playgame(PLAY):
x,y = 0,0
movex,movey = 0,0
charx=300
chary=200
direction = 'down'
enemyx= 10
enemyy=10
while PLAY:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == ord('m'):
pygame.mixer.music.stop()
if event.key == ord('n'):
pygame.mixer.music.play()
if event.key == ord('a'):
movex = -0.5
if event.key == ord('d'):
movex = 0.5
if event.key == ord('w'):
movey = -0.5
if event.key == ord('s'):
movey = 0.5
if event.type ==KEYUP:
if event.key == ord('a'):
movex = 0
if event.key == ord('d'):
movex = 0
if event.key == ord('w'):
movey = 0
if event.key == ord('s'):
movey = 0
if direction == 'down':
enemyy += 7
windowSurface.fill(BLUE)
windowSurface.blit(char, (charx, chary))
windowSurface.blit(enemy, (enemyx, enemyy))
pygame.display.update()
charx+=movex
chary+=movey
def playertopresskey(PLAY):
while True: # main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_SPACE:
PLAY = True
if PLAY == True:
playgame(PLAY)
pygame.display.update()
playertopresskey(PLAY)
I would like for the 'enemy' object to be able to fall from the top without the user either needing to keypress or to key up or to have to move the mouse on the screen, rather the 'enemy' would just fall from the top as soon as the subroutine is called.
You may need to tweak it a bit because it is set up for me at the moment but i deleted a few things that would give you bother. Hopefully someone can help me. Thanks.
The below is link to a picture similar to mine which you can download and replace in the code for both the 'char' and the 'enemy' variables to view this yourself for i cannot access the courses at the present time.
http://www.roleplaygateway.com/roleplay/the-five-elements/characters/miss-joy/image
I found your error, you would have caught it yourself if you would have divided your code into some functions. Here is the problem:
for event in pygame.event.get():
if event.type == QUIT:
...
if event.type == KEYDOWN:
...
if event.type == KEYUP:
...
if direction == 'down':
enemyy += 7
your code moving the enemy is called only when an event is waiting in the queue. Move it out of the loop, and you will be good to go.
You have to change indention for:
if direction == 'down':
enemyy += 7
Now it is inside for event in pygame.event.get():