Pygame UserPanel,Rotary,Boundaries [duplicate] - python

This question already has answers here:
Not letting the character move out of the window
(2 answers)
How do you get pygame to give warning when player touches side of screen?
(1 answer)
How to make ball bounce off wall with PyGame?
(1 answer)
Closed 2 years ago.
Firstly i need a userpanel. From there a user will enter 3 inputs. For example 1)What is your k? A)[USER İNPUT] 2) What is your M? B)[USER INPUT] 3)What is your a? C)[USER INPUT] whenever users apply these answers, pygame window will be open. The last answer will be our angle between the down platform of at the right side system and x direction. The other 2 answers will go arduino serial communation. Finally boundaries shoul be have. The circle can not pass up and down obstacles. Just pass from left side of the system. My code is below. Thanks four your answers,in advance :)
import pygame,sys
pygame.init()
win=pygame.display.set_mode((1030,650))
pygame.display.set_caption("Seri Manipulator Kontrolü")
x = 700
y = 300
width = 5
height = 0
vel = 5
oxu= 870
oyu= 420
owu= 160
ohu= 10
oxd= 870
oyd= 220
owd= 160
ohd= 10
centeredobx=870
centeredoby=230
centeredboy=190
centereden=10
cubukx= 880
cubuky= 320
cubuken= 140
cubukboy= 10
def yazdir():
win.fill((0,0,0))
pygame.draw.circle(win, (0, 127, 255), (x, y), width, 0)
pygame.draw.rect(win, (255, 0, 0), (oxu, oyu, owu, ohu))
pygame.draw.rect(win, (255, 0, 0), (oxd, oyd, owd, ohd))
pygame.draw.rect(win, (255, 255, 0), (centeredobx, centeredoby, centereden, centeredboy))
pygame.draw.rect(win, (128, 128, 128), (cubukx, cubuky, cubuken, cubukboy))
pygame.draw.rect(win, (255, 0, 0), (1020, 220, 10, 200))
pygame.display.update()
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
run = False
pygame.quit()
keys = pygame.key.get_pressed()
if pygame.key.get_pressed() and x in range(865,1025) and y in range(225,235) or pygame.key.get_pressed() and x in range(865,1025) and y in range(415,425):
return
else:
if keys[pygame.K_RIGHT] and 1010 > x > 860 and 235 <= y <= 415 and centeredobx <= 1005:
centeredobx += vel
cubukx += vel
cubuken -= vel
yazdir()
if keys[pygame.K_LEFT] and 1010 > x > 860 and 235 <= y <= 415 and centeredobx >= 875:
centeredobx -= vel
cubukx -= vel
cubuken += vel
yazdir()
if keys[pygame.K_LEFT] and x > 5:
x -= vel
yazdir()
if keys[pygame.K_RIGHT] and x < 1005:
x += vel
yazdir()
if keys[pygame.K_UP] and y > 5 :
y -= vel
yazdir()
if keys[pygame.K_DOWN] and y < 645:
y += vel
yazdir()
pygame.quit()

Related

How do I make a bullet shoot and hit system? [duplicate]

This question already has answers here:
How can i shoot a bullet with space bar?
(1 answer)
How do I detect collision in pygame?
(5 answers)
How do I stop more than 1 bullet firing at once?
(1 answer)
Closed 1 year ago.
This program currently makes two squares appear on the screen when you press start, the one on the left is controlled using WASD whilst the one on the right is controlled using the arrow keys. I want to make it so that when you click spacebar, the square on the left shoots a red rectangle (the bullet) to the right of the screen and if it hits the other square, then the health goes down by one. If it doesn't hit the other square, the bullet hits the edge of the screen and disappears (I would also like the other square to do this too but the other way around and instead of pressing space you press right control). The health variable for both squares is on lines 9 and 10. Can somebody please tell me how to do this?
Full Code (The two key presses that I would like to make the chosen character shoot are lines 60 and 72):
import pygame
import sys
import os
pygame.init()
FPS = 60
HEALTH_L = 3
HEALTH_R = 3
start_smallfont = pygame.font.SysFont('Corbel', 45)
start_text = start_smallfont.render('Start', True, (255, 255, 255))
rect_smallfont = pygame.font.SysFont('Corbel', 33)
rect_text = rect_smallfont.render('You', True, (255, 255, 255))
x = 630
y = 325
x2 = 170
y2 = 325
vel = 0.1
startWIDTH, startHEIGHT = 170, 80
screenWIDTH, screenHEIGHT = 800, 720
WIN = pygame.display.set_mode((screenWIDTH, screenHEIGHT))
pygame.display.set_caption(":D")
def main():
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if 800/2-85 <= mouse[0] <= 800/2-85+startWIDTH and 720/2-40 <= mouse[1] <= 720/2-40+startHEIGHT:
clock.tick(FPS)
run = False
while True:
global x, y, x2, y2, movingx
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
WIN.fill((0, 0, 0))
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
if keys[pygame.K_RIGHT] and x < screenWIDTH - 50 - vel:
x += vel
if keys[pygame.K_UP] and y > vel:
y -= vel
if keys[pygame.K_DOWN] and y < screenHEIGHT - 50 - vel:
y += vel
if keys[pygame.K_SPACE]:
#PUT BULLET FUNCTION HERE
if keys[pygame.K_a] and x2 > vel:
x2 -= vel
if keys[pygame.K_d] and x2 < screenWIDTH - 50 - vel:
x2 += vel
if keys[pygame.K_w] and y2 > vel:
y2 -= vel
if keys[pygame.K_s] and y2 < screenHEIGHT - 50 - vel:
y2 += vel
if keys[pygame.K_RCTRL]:
#PUT BULLET FUNCTION HERE
pygame.draw.rect(WIN, (255, 0, 0), (x, y, 50, 50))
pygame.draw.rect(WIN, (255, 0, 0), (x2, y2, 50, 50))
pygame.display.update()
mouse = pygame.mouse.get_pos()
if 800/2-85 <= mouse[0] <= 800/2-85+startWIDTH and 720/2-40 <= mouse[1] <= 720/2-40+startHEIGHT:
pygame.draw.rect(WIN, (255, 91, 91), (800/2-85, 720/2-40, startWIDTH, startHEIGHT))
else:
pygame.draw.rect(WIN, (255, 0, 0), (800/2-85, 720/2-40, startWIDTH, startHEIGHT))
WIN.blit(start_text, (800/2-85+42,720/2-40+20))
pygame.display.update()
if __name__ == "__main__":
main()

Pygame game code - character stopped moving

I am creating a simple Pygame game and the character was moving around the screen when I pressed the arrow keys. I have added an intro which has the title and 2 buttons and when the green button is pressed to start the game, the game loads but the character no longer moves when the keys are pressed. Can someone please help tell me why the arrow keys no longer move the character? Thank you!
Code:
import time
#we need to initiate pygame at the start of all our code
pygame.init()
display_width = 800
display_height = 600
#creating window, in tuple is width and height of screen
win = pygame.display.set_mode((display_width, display_height))
x = (display_width * 0.45)
y = (display_height * 0.8)
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
bright_red = (255, 0, 0)
bright_green = (0,255,0)
def crash():
message_display('Item collected')
#button
def button(msg,x,y,w,h,ic,ac, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
#print(mouse)
if x + w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(win, ac, (x,y,w,h))
if click[0] == 1 and action != None:
action()
def collect_item():
message_display('Do you want to pick up item?')
button("YES",150,450,100,50,green, bright_green, game_loop)
button("NO",550,450,100, 50, red, bright_red, game_loop)
#def game_loop():
#x = (display_width * 0.45)
#y = (display_height * 0.8)
#x_change = 0
#dodged = 0
#run = True
#good idea to create a screen width variable
screenWidth = 800
#Name of our window
pygame.display.set_caption("First Game")
#Code for importing multiple images of the animated sprite
#walk right animation
walkRight = [pygame.image.load('R1.PNG'), pygame.image.load('R2.PNG'), pygame.image.load('R3.PNG')]
#walk left animation
walkLeft = [pygame.image.load('L1.PNG'), pygame.image.load('L2.PNG'), pygame.image.load('L3.PNG')]
#back ground image load in
bg = pygame.image.load('grass11.jpg')
#Basic standing sprite, it is the still image. shows this character when they are not moving
char = pygame.image.load('front.PNG')
def puff(x,y):
win.blit(char (x,y))
#allows us to change our fps in the game
clock = pygame.time.Clock()
swordIMG = pygame.image.load('smallsword.png')
staffIMG = pygame.image.load('staff.png')
chestIMG = pygame.image.load('chest.png')
coinIMG = pygame.image.load('coin.png')
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf', 115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2)), ((display_height/2))
win.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
def quitgame():
pygame.quit()
quit()
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
win.fill(white)
largeText = pygame.font.Font('freesansbold.ttf', 115)
TextSurf, TextRect = text_objects("TITLE", largeText)
TextRect.center = ((display_width/2)), ((display_height/2))
win.blit(TextSurf, TextRect)
#Button
button("GO!",150,450,100,50,green, bright_green, game_loop)
button("Quit",550,450,100, 50, red, bright_red, quitgame)
pygame.display.update()
#creating character
#x = 50
#y = 400
#width and height of sprite
width = 100
height = 100
#staff
staffwidth = 94
staffheight = 106
#coin
coinwidth = 74
coinheight = 74
#chest
chestwidth = 84
chestheight = 84
#velocity is how fast the character moves
vel = 5
left = False
right = False
walkCount = 0
#function which redraws the game window, this area is for drawing, we do not draw in main loop
def redrawGameWindow():
#x = (display_width * 0.45)
#y = (display_height * 0.8)
global walkCount
win.blit(bg, (0,0)) #back ground image
win.blit(swordIMG,(600,400))
win.blit(staffIMG, (70, 60))
win.blit(chestIMG, (600, 100))
win.blit(coinIMG, (350,300))
if walkCount + 1 >= 0:
walkCount = 0
if left:
win.blit(walkLeft[walkCount], (x,y)) #displaying walk left sprite
walkCount += 1
elif right:
win.blit(walkRight[walkCount], (x,y))
walkCount += 1
#repeat for up and down
else:
win.blit(char, (x,y)) #if we are not moving we blit our character
pygame.display.update() #if we want something to show on the screen in pygame, we must update the screen
#main loop for program
#main loop
#run the variable
#def game_loop():
#redrawGameWindow()
#x = (display_width * 0.45)
#y = (display_height * 0.8)
#x_change = 0
#dodged = 0
def game_loop():
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
dodged = 0
run = True
while run:
#redrawGameWindow()
#game_intro()
clock.tick(27) #sets fps to 20 seconds
#pygame.time.delay(100) #clock in pgyame, parameter is milliseconds
for event in pygame.event.get(): #event is what player does eg. mouse click or key press
if event.type == pygame.QUIT: #if they click the x button (quit)
run = FALSE #loop = false
#using arrow keys to move shape
# all of the and's mean the shape cannot move off the screen
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
left = True
right = False
elif keys[pygame.K_RIGHT] and x < 800 - width - vel: #screen width - width of character
x += vel
right = True
left = False
elif keys[pygame.K_UP] and y > vel:
y -= vel
up = True
down = False
elif keys[pygame.K_DOWN] and y < 600 - height - vel:
y += vel
down = True
up = False
else:
right = False
left = False
up = False
down = False
walkCount = 0
if x < 100 - vel and x > 50 - vel and y > 40 - vel and y < 70:
crash()
if x > 600 - vel and x < 703 - vel and y > 400 - vel and y < 502 - vel:
crash()
if x > 330 - vel and x < 420 - vel and y > 280 - vel and y < 300 - vel:
crash()
if x > 600 - vel and x < 684 - vel and y > 100 - vel and y < 184 - vel:
crash()
redrawGameWindow()
#if y < 160 - vel and y > 90 - vel:
#crash()
game_intro()
#game_loop()
#redrawGameWindow() #call function
pygame.quit #game ends
The variables x, y, right, left, up, down and walkCount are variables in global name space. You have to use the global statement, if you want to write to the variables in global namespace, from within the function game_loop:
def game_loop():
global x, y, left, right, up, down, walkCount
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
dodged = 0
run = True
while run:
# [...]
Furthermore, the buttons are not drawn, if the mouse is not on the buttons:
def button(msg,x,y,w,h,ic,ac, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
pygame.draw.rect(win, ic, (x,y,w,h)) # <--- This line is missing
if x + w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(win, ac, (x,y,w,h))
if click[0] == 1 and action != None:
action()

There is an error Decrypting an integer is required

I have been following this tutorial to build a simple 2D game using pygame.
I have checked multiple times my code but I still can't figure out what this error means
import pygame as pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
pygame.display.set_caption('First Game')
pygame.display.update()
Here I set up all commands in response to the user's imput
walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'),
pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'),
pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]
walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'),
pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'),
pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]
bg = pygame.image.load('bg.jpg')
char = pygame.image.load('standing.png')
clock = pygame.time.Clock()
x = 50
y = 50
width = 64
height = 64
vel = 5
isJump = False
jumpCount = 10
left = False
right = False
walkCount = 0
run = True
Here I defined the frames and the corresponding pictures with each movement. The problem is coming from here but I don't understand why
def redrawgamewindow():
global walkCount
window.blit(bg, (0, 0))
pygame.draw.rect(window, (255, 255, 255), (x, y, width, height))
if walkCount + 1 >= 27:
walkCount = 0
if left:
window.blit(walkLeft[walkCount // 3], (x, y))
walkCount += 1
elif right:
window.blit(walkRight[walkCount // 3], (x, y))
walkCount += 1
else:
window.blit(char, (x, y))
walkCount = 0
pygame.display.update()
while run:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
right = False
left = True
elif keys[pygame.K_RIGHT] and x < 500 - (width + vel):
x += vel
right = True
left = False
else:
right = False
left = False
walkCount = 0
if not isJump:
if keys[pygame.K_UP] and y > vel:
y -= vel
if keys[pygame.K_DOWN] and y < 500 - (height + vel):
y += vel
if keys[pygame.K_SPACE]:
isJump = True
right = False
left = False
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount * abs( jumpCount))*0.5
jumpCount -= 1
else:
isJump = False
jumpCount = 10
redrawgamewindow()
pygame.quit()
The error displayed is
/Users/Thomas.V/Documents/Documents/Perso/Coding /Python /Pycharm Projects/Pygame.py:40: DeprecationWarning: an integer is required (got type float). Implicit conversion to integers using __int__ is deprecated and may be removed in a future version of Python.
pygame.draw.rect(window, (255, 255, 255), (x, y, width, height))
The warning is caused because y is not an integral value, because of
y -= (jumpCount * abs( jumpCount))*0.5
Get rid of the warning by rounding y (see round):
pygame.draw.rect(window, (255, 255, 255), (x, round(y), width, height))

How can I add a sprite when no keys are pressed?

How can I add a sprite when no keys are pressed? I have each four directions covered when the arrow keys are pressed but when they are released the sprite goes obviously but cant think of how to add it. I tried adding else statements and other things best thing i got was the standing forward sprite being underneath the others but cant seem to get it to go when one of the arrow keys is pressed and return when they are relased.
Any help appreciated. Thanks in advance.
my code:
import pygame
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (200, 0, 0)
GREEN = (0, 200, 0)
BLUE = (0, 0, 200)
# background
background = pygame.image.load('Playing Game state 2.png')
# character
standingforward = pygame.image.load('standingforward.png')
standingdownward = pygame.image.load('standingdownwards.png')
standingleft = pygame.image.load('standingleft.png')
standingright = pygame.image.load('standingright.png')
# player variables
x = 375
y = 525
w = 50
h = 50
vel = 0.5
screenWidth = 800
screenHeight = 575
screen = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption("FROGGER")
sprite = pygame.draw.rect
running = True
while running:
# sprites
screen.blit(background, (0, 0))
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
screen.blit(standingleft, (x, y))
if keys[pygame.K_RIGHT]:
screen.blit(standingright, (x, y))
if keys[pygame.K_DOWN]:
screen.blit(standingdownward, (x, y))
if keys[pygame.K_UP]:
screen.blit(standingforward, (x, y))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
# controls
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= vel
if x < 0:
x = 0
if keys[pygame.K_RIGHT]:
x += vel
if x > screenWidth-w:
x = screenWidth-w
if keys[pygame.K_UP]:
y -= vel
if y < 0:
y = 0
if keys[pygame.K_DOWN]:
y += vel
if y > screenHeight-h:
y = screenHeight-h
pygame.quit()
Add a variable that refers to the current image. Change the variable when a key is pressed. Draw the current image in the application loop:
current_image = standingright
running = True
while running:
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
current_image = standingleft
if keys[pygame.K_RIGHT]:
current_image = standingright
if keys[pygame.K_DOWN]:
current_image = standingdownward
if keys[pygame.K_UP]:
current_image = standingforward
screen.blit(background, (0, 0))
screen.blit(current_image, (x, y))
# [...]

The character I created doesn't come back to the exact platform after a single trigger press on UP arrow key

I am a Pygame beginner. I started to build a game on jumping dinosaur. The character I created doesn't come back to the exact platform after a single trigger press on UP arrow key. It lands slightly above the platform and its lands perfectly after 2,3 UP arrow presses.
I am a complete beginner.
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
x = 20
y = 400
width = 30
height = 45
vel = 10
black = (0,0,0)
x1 = 20
y1 = 30
white = (255,255,255)
run = True
while run:
pygame.time.delay(20)
win.fill(black)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x>=20:
x -= vel
if keys[pygame.K_RIGHT]and x<=450:
x += vel
if keys[pygame.K_UP] and y>=20:
y -= vel
if keys[pygame.K_DOWN] and y<=390:
y += vel
#gravity
else:
if not keys[pygame.K_UP] and y <= 400:
y += 30
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.draw.line(win,white,[0,472],[472,472],2)
pygame.display.update()
pygame.display.flip()
pygame.quit()
Your gravity application is broken. There is plenty of ways to fix this, my choice here is to always apply gravity but limit y so that it can never go bigger than the height of the character above the ground-plane.
To elaborate on what's actually wrong with your approach: your jumps create upwards motion incremented by vel. Which is only a fraction of gravity. So pushing the character up e.g. 40 pixels, and then pull it down by 30 pixels leaves you 10 pixels above ground. Only if you manage to precisely rise exactly a multiple of gravity, you will fall back.
My solution always falls down but then limits the position to the one where the character is right on the ground level.
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
x = 20
y = 400
width = 30
height = 45
black = (0,0,0)
x1 = 20
y1 = 30
white = (255,255,255)
bottom = 472
gravity = 30
vel = 10 + gravity
run = True
while run:
pygame.time.delay(20)
win.fill(black)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x>=20:
x -= vel
if keys[pygame.K_RIGHT]and x<=450:
x += vel
if keys[pygame.K_UP] and y>=20:
y -= vel
if keys[pygame.K_DOWN] and y<=390:
y += vel
else:
y = min(bottom - height, y + gravity)
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.draw.line(win,white,[0,bottom],[bottom,bottom],2)
pygame.display.update()
pygame.display.flip()
pygame.quit()

Categories

Resources